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

📄 charu.java

📁 用户需要注册
💻 JAVA
字号:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
//对goods表的操作
public class ChaRu {
	public static final String INSERTUSER = "insert into  goods(name,goodsname,number,price) values(?,?,?,?)";
	public static boolean insertUser(String name, String goodsname,double number,double price)
    {//添加商品
        boolean flag = false;
        Connection conn = null;
        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);
   	 	}
   	 	catch(SQLException e){
   	 		System.out.println(e.getMessage());
   	 	}
        PreparedStatement pstmt = null;
        int count = 0;
        try
        {
            pstmt = conn.prepareStatement(INSERTUSER);
            pstmt.setString(1, name);
            pstmt.setString(2, goodsname);
            pstmt.setDouble(3, number);
            pstmt.setDouble(4,price);
            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 updateGoods(String name,String goodsname,double number,double price)
	{//添加已存在的商品
		boolean flag = false;
        Connection conn = null;
        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());
   	 	}
   	 	PreparedStatement pstmt = null;
   	 	int count = 0;
   	 	try
   	 	{
   	 		pstmt = conn.prepareStatement("update goods set number=?,price=? where name='"+name+"'and goodsname='"+goodsname+"'");
   	 		pstmt.setDouble(1, number);
   	 		pstmt.setDouble(2, price);
         	count = pstmt.executeUpdate();
         if (count != 0)
         {
             flag = true;
         }
         //conn.commit();
   	 	}
   	 	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 void main(String [] args){
		//System.out.println(updateGoods("a","e",2,4));
	}

}

⌨️ 快捷键说明

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