其实,也就那几步而已:
别忘了,加载jar文件
加载驱动
获得Connection
获得PreparedStatement /PreparedStatement
执行sql语句
处理结果集ResultSet
关闭连接
mport java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import Entity.JIAJU;public class JiaJu { public JIAJU selectExe(int shouhinId) { JIAJU jia = new JIAJU(); try { Connection con = ConnectionManager.getConnection(); String sql = "select * from jiaju where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, shouhinId); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { jia.setShouhinId(rs.getInt("shouhinId")); jia.setShouhinName(rs.getString("shouhinName")); jia.setShouhinColor(rs.getString("shouhinColor")); jia.setShouhinPrice(rs.getInt("shouhinPrice")); jia.setShouhinPai(rs.getString("shouhinPai")); jia.setShouhinShi(rs.getString("shouhinShi")); // list.add(jia); } } } catch (Exception e) { e.printStackTrace(); } return jia; } public void insertJia(JIAJU jia) { try { Connection con = ConnectionManager.getConnection(); String sql = "insert into jiaju values(?,?,?,?,?)"; PreparedStatement ps = con.prepareStatement(sql); ps.setString(1, jia.getShouhinName()); ps.setString(2, jia.getShouhinColor()); ps.setInt(3, jia.getShouhinPrice()); ps.setString(4, jia.getShouhinPai()); ps.setString(5, jia.getShouhinShi()); ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } } public List selectJia() { List list = new ArrayList(); try { Connection con = ConnectionManager.getConnection(); String sql = "select * from jiaju "; PreparedStatement ps = con.prepareStatement(sql); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { JIAJU jia = new JIAJU(); jia.setShouhinId(rs.getInt("shouhinId")); jia.setShouhinName(rs.getString("shouhinName")); jia.setShouhinColor(rs.getString("shouhinColor")); jia.setShouhinPrice(rs.getInt("shouhinPrice")); jia.setShouhinPai(rs.getString("shouhinPai")); jia.setShouhinShi(rs.getString("shouhinShi")); list.add(jia); } } } catch (Exception e) { e.printStackTrace(); } return list; } public JIAJU selectbuy(int shouhinId) { JIAJU jia = new JIAJU(); try { Connection con = ConnectionManager.getConnection(); String sql = "select * from jiaju where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, shouhinId); ResultSet rs = ps.executeQuery(); if (rs != null) { while (rs.next()) { jia.setShouhinId(rs.getInt("shouhinId")); jia.setShouhinName(rs.getString("shouhinName")); jia.setShouhinColor(rs.getString("shouhinColor")); jia.setShouhinPrice(rs.getInt("shouhinPrice")); jia.setShouhinPai(rs.getString("shouhinPai")); jia.setShouhinShi(rs.getString("shouhinShi")); } } } catch (Exception e) { e.printStackTrace(); } return jia; } public void updateLou(JIAJU jia){ try{ Connection con = ConnectionManager.getConnection(); String sql = "update jiaju set shouhinPrice=? where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1,jia.getShouhinPrice()); ps.setInt(2, jia.getShouhinId()); ps.executeUpdate(); }catch(Exception e){ e.printStackTrace(); } } public void deleteLou(JIAJU jia){ try{ Connection con = ConnectionManager.getConnection(); String sql = "delete from jiaju where shouhinId=?"; PreparedStatement ps = con.prepareStatement(sql); ps.setInt(1, jia.getShouhinId()); ps.executeUpdate(); }catch(Exception e){ e.printStackTrace(); } }}
取消
评论