From 8f3d975a3f7ea11d864aef3d8e0de1f7dcc20f7d Mon Sep 17 00:00:00 2001 From: nxist2202005014 <1463859337@qq.com> Date: Wed, 26 Jun 2024 09:49:06 +0800 Subject: [PATCH] ADD file via upload --- OrderDetailDaoImpl.java | 160 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 OrderDetailDaoImpl.java diff --git a/OrderDetailDaoImpl.java b/OrderDetailDaoImpl.java new file mode 100644 index 0000000..6436b4c --- /dev/null +++ b/OrderDetailDaoImpl.java @@ -0,0 +1,160 @@ +package flowershop.daoimpl; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import flowershop.dao.OrderDetailDao; +import flowershop.model.OrderDetail; + +public class OrderDetailDaoImpl implements OrderDetailDao { + + @Override + public List findAll() { + //通过执行SQL语句查询所有订单详细信息,并将结果封装成OrderDetail对象的列表返回。 + String sql = "select * from account"; + List list = new ArrayList(); + try( //2.创建数据库连接 + Connection conn = Jdbc.getConnection(); + //3.创建语句对象 + PreparedStatement pstmt = conn.prepareStatement(sql); + //4.绑定参数 + //5.执行查询 + ResultSet rs = pstmt.executeQuery(); ){ + //6.遍历结果集 + while(rs.next()) { + OrderDetail detail = new OrderDetail(); + detail.setOrderid(rs.getLong("orderid")); + detail.setProductid(rs.getString("productid")); + detail.setQuantity(rs.getInt("quantity")); + detail.setPrice(rs.getDouble("price")); + list.add(detail); + } + }catch (SQLException e) { + e.printStackTrace(); + } + return list; + } + + + @Override + public OrderDetail findByPk(int orderid, String productid) { + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + String sql = "select orderid,productid,quantity,unitcost from ordersdatail where orderid=? and productid=?"; + try { + //2.创建数据库连接 + conn = Jdbc.getConnection();//1.加载驱动程序 + //3.创建语句对象 + pstmt= conn.prepareStatement(sql); + //4.绑定参数 + pstmt.setInt(1, orderid); + pstmt.setString(2,productid); + //5.执行查询 + rs = pstmt.executeQuery(); + //6.遍历按主键查询 + if(rs.next()) { + OrderDetail detail = new OrderDetail(); + detail.setOrderid(rs.getLong("orderid")); + detail.setProductid(rs.getString("productid")); + detail.setQuantity(rs.getInt("quantity")); + detail.setPrice(rs.getDouble("price")); + return detail; + } + }catch(SQLException e) { + e.printStackTrace(); + }finally { + if (rs !=null) { + try { + rs.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (pstmt !=null) { + try { + pstmt.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + if (conn !=null) { + try { + conn.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + } + } + return null; + } + + + @Override + public int create(OrderDetail orderDetail) { + String sql = "insert into orderdetail(orderid,productid,quantity,price) values(?,?,?,?)"; + try (// 2.创建数据库连接 + Connection conn = Jdbc.getConnection(); + // 3.创建语句对象 + PreparedStatement pstmt = conn.prepareStatement(sql);) { + // 4.绑定参数 + pstmt.setLong(1, orderDetail.getOrderid()); + pstmt.setString(2, orderDetail.getProductid()); + pstmt.setInt(3, orderDetail.getQuantity()); + pstmt.setDouble(4, orderDetail.getPrice()); + // 5.执行 + int a = pstmt.executeUpdate(); + System.out.printf("成功插入%d数据.\n",a); + } catch (SQLException e) { + // TODO 自动生成的 catch 块 + e.printStackTrace(); + } + return 0; + } + + + @Override + public int modify(OrderDetail orderDetail) { + String sql = "update ordersdetail set price = ? where orderid = ? and productid = ?"; + try ( // 2.创建数据库连接 + Connection conn = Jdbc.getConnection(); + // 3. 创建语句对象 + PreparedStatement pstmt= conn.prepareStatement(sql) + ) { + // 4. 绑定参数 + pstmt.setDouble(1, orderDetail.getPrice()); + pstmt.setLong(2, orderDetail.getOrderid()); + pstmt.setString(3, orderDetail.getProductid()); + // 5. 执行修改(C、U、D) + int affectedRows = pstmt.executeUpdate(); + System.out.printf("成功更新%d条数据。\n", affectedRows); + } catch (SQLException e) { + e.printStackTrace(); + } + return 0; + } + + + @Override + public int remove(OrderDetail orderDetail) { + String sql = "delete from ordersdetail where orderid = ? and productid = ?"; + try ( // 2.创建数据库连接 + Connection conn = Jdbc.getConnection(); + // 3. 创建语句对象 + PreparedStatement pstmt = conn.prepareStatement(sql)) { + // 4. 绑定参数 + pstmt.setLong(1, orderDetail.getOrderid()); + pstmt.setString(2, orderDetail.getProductid()); + // 5. 执行修改(C、U、D) + int affectedRows = pstmt.executeUpdate(); + System.out.printf("成功删除%d条数据。\n", affectedRows); + } catch (SQLException e) { + e.printStackTrace(); + } + return 0; + } +}