连接数据库方法
public class DButil { private DButil(){} static Connection conn; static{ try{ Class.forName("com.mysql.jdbc.Driver"); String url="jdbc:mysql://localhost:3306/store"; String username="root"; String password="123456"; conn=DriverManager.getConnection(url,username,password); }catch(Exception ex){ throw new RuntimeException(ex+"数据库连接失败"); } } public static Connection getConn(){ return conn; } public static void close(Statement sta,Connection conn){ if(sta!=null){ try{ sta.close(); }catch(SQLException ex){} } if(conn!=null){ try{ conn.close(); }catch(SQLException ex){} } } public static void close(ResultSet rs,Statement sta){ if(rs!=null){ try{ rs.close(); }catch(SQLException ex){} } if(sta!=null){ try{ sta.close(); }catch(SQLException ex){} } } }
通过此代码连接数据库,也就是DButil类
然后进行对数据库增删改查的操作,首先先写好sql语句,然后用PreparedStatement pst = conn.prepareStatement(sql);准备好执行sql语句,通过pst.set()方法 给sql里面的问号传值,最后用pst.executeUpdate()来完成增删改的操作,其返回的是int。
查询的话是pst.executeQuery(),返回的是ResultSe结果集;具体代码如下(商店管理系统)、
public class StoreApp { public static void main(String[] args) throws SQLException { ArrayList<Store> list = new ArrayList<Store>(); while(true){ Connection conn = DButil.getConn(); switch(show()){ case 1: selectMethod(list,conn); break; case 2: addMethod(conn); break; case 3: modifyMethod(conn); break; case 4: deleteMethod(conn); break; case 5: conn.close(); return; } } } public static int show(){ System.out.println("------------商城管理系统------------"); System.out.println("1.查询商品信息"); System.out.println("2.新增商品信息"); System.out.println("3.修改商品信息"); System.out.println("4.删除商品信息"); System.out.println("5.退出"); System.out.println("请输入您的选择"); Scanner sc = new Scanner(System.in); return sc.nextInt(); } public static void selectMethod(ArrayList<Store> list,Connection conn) throws SQLException{ String sql = "select * from store"; PreparedStatement pst = conn.prepareStatement(sql); ResultSet rs = pst.executeQuery(); while(rs.next()){ Store s= new Store(); s.setSid(rs.getInt("sid")); s.setBrands(rs.getString("brands")); s.setSize(rs.getInt("size")); s.setPrice(rs.getDouble("price")); s.setCounts(rs.getInt("counts")); list.add(s); } for(int i =0;i<list.size();i++){ System.out.println("商品编号为"+list.get(i).getSid()+"商品名称为"+list.get(i).getBrands()+"商品大小为"+list.get(i).getSize()); } DButil.close(rs, pst); } public static void addMethod(Connection conn) throws SQLException{ String sql = "INSERT INTO store(brands,size,price,counts) VALUE (?,?,?,?)"; PreparedStatement pst = conn.prepareStatement(sql); Scanner sc =new Scanner(System.in); System.out.println("请输入商品名称"); String brands = sc.next(); System.out.println("请输入商品尺寸"); int size = sc.nextInt(); System.out.println("请输入商品价格"); double price = sc.nextDouble(); System.out.println("请输入商品数量"); int counts = sc.nextInt(); pst.setString(1,brands); pst.setInt(2, size); pst.setDouble(3, price); pst.setInt(4, counts); int a = pst.executeUpdate(); if(a!=0){ System.out.println("添加成功"); } pst.close(); } public static void modifyMethod(Connection conn) throws SQLException{ String sql = "UPDATE store SET PRICE = ? WHERE sid = ?"; PreparedStatement pst = conn.prepareStatement(sql); Scanner sc = new Scanner(System.in); System.out.println("请输入您要修改的产品的id"); int sid = sc.nextInt(); System.out.println("请输入修改后的价格"); Double price = sc.nextDouble(); pst.setDouble(1, price); pst.setInt(2, sid); int a = pst.executeUpdate(); if(a!=0){ System.out.println("修改成功"); } pst.close(); } public static void deleteMethod(Connection conn) throws SQLException{ String sql = "delete from store where sid = ?"; Scanner sc = new Scanner(System.in); System.out.println("请输入您要删除产品的id"); int sid = sc.nextInt(); PreparedStatement pst = conn.prepareStatement(sql); pst.setInt(1, sid); int a = pst.executeUpdate(); if(a!=0){ System.out.println("删除成功"); } pst.close(); } }
原文地址:https://www.cnblogs.com/wangyufei123/p/8745701.html
时间: 2024-10-28 21:00:13