package DAO; import java.sql.Connection; import java.sql.Date; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import DBConn.DBConn; import MySQL.Purchase; public class PurchaseImpl implements PurchaseDao{ //增加 public void add(Purchase p) { String ssql = "insert into purchase values(?,?,?,?)"; Connection conn = DBConn.open(); try { PreparedStatement pstmt = conn.prepareStatement(ssql); pstmt.setString(1,p.getId()); pstmt.setInt(2,p.getNumber()); pstmt.setInt(3,p.getPrice()); pstmt.setDate(4,p.getDate()); pstmt.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); }finally{ DBConn.close(conn); } } //删除 public void delete(String id) { String ssql = "delete from purchase where id=?"; Connection conn = DBConn.open(); try { PreparedStatement pstmt = conn.prepareStatement(ssql); pstmt.setString(1,id); pstmt.executeUpdate(); } catch (SQLException e){ e.printStackTrace(); }finally{ DBConn.close(conn); } } //通过id得到商品 public Purchase getPurchaseById(String id) { String ssql = "select * from purchase where id=?"; Connection conn = DBConn.open(); try { PreparedStatement pstmt = conn.prepareStatement(ssql); pstmt.setString(1, id); ResultSet rs = pstmt.executeQuery(); if(rs.next()){ int number = rs.getInt(2); int price = rs.getInt(3); Date date = rs.getDate(4); Purchase p = new Purchase(); p.setId(id); p.setNumber(number); p.setPrice(price); p.setDate(date); return p; } } catch (SQLException e){ e.printStackTrace(); }finally{ DBConn.close(conn); } return null; } //查询 public String query() { String ssql = "select * from purchase"; Connection conn = DBConn.open(); try { Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(ssql); String s =""; while(rs.next()){ String id = rs.getString(1); int number = rs.getInt(2); int price = rs.getInt(3); Date date = rs.getDate(4); s = s+id+" "+number+" "+price+" "+date+"\n"; } return s; }catch (SQLException e1){ e1.printStackTrace(); }finally{ DBConn.close(conn); } return null; } }
最近下载更多
sunshine9920 LV12
2023年10月22日
微信网友_6191697646571520 LV6
2022年10月31日
testuser1234567 LV24
2022年5月31日
ming_123_9715 LV23
2022年5月3日
xinxin224 LV3
2022年3月14日
dfz12345 LV4
2021年12月31日
2089675149 LV7
2021年12月23日
孙纪龙啊 LV10
2021年12月12日
1532871844 LV3
2021年12月3日
zrq1025 LV11
2021年8月23日
最近浏览更多
akittyboy LV9
3月22日
1134116035
2月28日
暂无贡献等级
fff2003 LV6
1月11日
yenaever
1月4日
暂无贡献等级
求学的熊猫 LV6
2023年12月25日
微信网友_6786215447367680 LV4
2023年12月21日
李朝磊 LV18
2023年12月3日
LTPR66 LV1
2023年12月1日
冀小鹏
2023年11月29日
暂无贡献等级
1143011510 LV15
2023年11月27日