⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 charucar.java

📁 用户需要注册
💻 JAVA
字号:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//对shopcar表的操作
public class ChaRuCar {
	public static final String INSERTUSER = "insert into  shopcar(name,goodsname,number,price,shopkeepername,state) values(?,?,?,?,?,?)";
	
	public static boolean insertUser(String name, String goodsname,double number,double price,String shopkeepername,int state)
    {//商品加入购物车
        boolean flag = false;
        Connection conn = null;
        Statement stmt = null;
		ResultSet rs=null;
		String s=null;
		double remain=0.0;
        String username="root";
   	 	String password="111111";
   	 	String url = "jdbc:mysql://localhost:3306/shoppingmall";
   	 try{  
   		 //-------------加载驱动-------------------------   
   		 Class.forName("org.gjt.mm.mysql.Driver").newInstance();
   		 System.out.println("Success loading Mysql Driver!");
   	 }
   	 catch(Exception e){
   		 System.out.println("Error loading Mysql Driver!");
   	 }
   	 try{   
   		 conn = DriverManager.getConnection(url,username,password);
   		// System.out.println("Success connect Mysql Database!"); 
   		}
	 catch(SQLException e){
		 System.out.println(e.getMessage());
	 }
	 try//修改商店中该商品的数量
		{   
			conn = DriverManager.getConnection(url,username,password);
			stmt = conn.createStatement();  //Statement对象用于将sql语句发送到数据库中 
			rs = stmt.executeQuery("Select number FROM goods where name = '"+shopkeepername +"'and goodsname='"+goodsname +"'");
			while(rs.next())
			{
				s = rs.getString("number").trim();
			}
			remain = Double.parseDouble(s)-number;
		}
		catch(SQLException e)
		{
			System.out.println(e.getMessage());
		}
		if(remain<0)//库存不够
			flag=false;
		else
		{
			PreparedStatement pstmt = null;
			int count = 0;
        try
		{   
			pstmt = conn.prepareStatement("update goods set number=? where name = '"+shopkeepername +"'and goodsname='"+goodsname +"'");
			pstmt.setDouble(1, remain);
			pstmt.executeUpdate();
		}
		catch(SQLException e)
		{
			System.out.println(e.getMessage());
		}
        try
        {
            pstmt = conn.prepareStatement(INSERTUSER);
            pstmt.setString(1, name);
            pstmt.setString(2, goodsname);
            pstmt.setDouble(3, number);
            pstmt.setDouble(4,price);
            pstmt.setString(5, shopkeepername);
            pstmt.setInt(6, state);
            count = pstmt.executeUpdate(); // 记录操作执行的记录条数
            if (count != 0)
            {
                flag = true;
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != conn)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
            if (null != pstmt)
            {
                try
                {
                    pstmt.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        }
		}
        return flag;
    }
	public static boolean deletegoods(String name, String goodsname,String shopkeepername)
	{//重新设定商店中商品的数量
		boolean flag = false;
        Connection conn = null;
        Statement stmt = null;
		ResultSet rs=null;
		String s=null;
		double remain=0.0;
		double all=0.0;
        String username="root";
   	 	String password="111111";
   	 	String url = "jdbc:mysql://localhost:3306/shoppingmall";
   	 try{  
   		 //-------------加载驱动-------------------------   
   		 Class.forName("org.gjt.mm.mysql.Driver").newInstance();
   		 System.out.println("Success loading Mysql Driver!");
   	 }
   	 catch(Exception e){
   		 System.out.println("Error loading Mysql Driver!");
   	 }
   	 try{   
   		 conn = DriverManager.getConnection(url,username,password);
   		 System.out.println("Success connect Mysql Database!"); 
   		}
	 catch(SQLException e){
		 System.out.println(e.getMessage());
	 }
	 try
		{   //修改商店中该商品的数量
			conn = DriverManager.getConnection(url,username,password);
			stmt = conn.createStatement();  //Statement对象用于将sql语句发送到数据库中 
			rs = stmt.executeQuery("Select number FROM shopcar where name = '"+name +"'and goodsname='"+goodsname +"'");
			while(rs.next())
			{
				s = rs.getString("number").trim();
			}
			all = Double.parseDouble(s);
		}
		catch(SQLException e)
		{
			System.out.println(e.getMessage());
		}
		 try
			{   
				conn = DriverManager.getConnection(url,username,password);
				stmt = conn.createStatement();  //Statement对象用于将sql语句发送到数据库中 
				rs = stmt.executeQuery("Select number FROM goods where name = '"+shopkeepername +"'and goodsname='"+goodsname +"'");
				while(rs.next())
				{
					s = rs.getString("number").trim();
				}
				remain = Double.parseDouble(s)+all;
				System.out.println(remain);
			}
			catch(SQLException e)
			{
				System.out.println(e.getMessage());
			}
		PreparedStatement pstmt = null;
		try
		{   
			pstmt = conn.prepareStatement("update goods set number=? where name = '"+shopkeepername +"'and goodsname='"+goodsname +"'");
			pstmt.setDouble(1, remain);
			pstmt.executeUpdate();
			flag=true;
		}
		catch(SQLException e)
		{
			System.out.println(e.getMessage());
		}
		finally
        {
            if (null != conn)
            {
                try
                {
                    conn.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
            if (null != pstmt)
            {
                try
                {
                    pstmt.close();
                }
                catch (SQLException e)
                {
                    e.printStackTrace();
                }
            }
        }
		return flag;
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -