package com.mbz.dao.impl; 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 com.mbz.dao.OrdersDetailDaombz; import com.mbz.model.OrdersDetailmbz; import com.mbz.model.Productmbz; public class OrdersDetailDaoImplmbz implements OrdersDetailDaombz{ @Override public List findAll() { OrdersDetailmbz ordersDetailmbz = new OrdersDetailmbz(); List list = new ArrayList(); try { Connection connection = DBHelper.getConnection(); String sql = "select * from ordersdetail "; PreparedStatement pstm = connection.prepareStatement(sql); ResultSet rs = pstm.executeQuery(); // 6.遍历结果集 if (rs.next()) { ordersDetailmbz = new OrdersDetailmbz(); ordersDetailmbz.setOrderidmbz(rs.getLong("orderidl")); ordersDetailmbz.setProductidmbz(rs.getString("productid")); ordersDetailmbz.setQuantitymbz(rs.getInt("quantity")); ordersDetailmbz.setUnitcostmbz(rs.getDouble("unitcostmbz")); } else { System.out.println("当前用户不存在"); rs.close(); pstm.close(); connection.close(); } } catch (Exception e) { e.printStackTrace(); } return list; } @Override public OrdersDetailmbz findByPK(int orderid, String productid) { Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; Productmbz productmbz = null; try { Connection connection = DBHelper.getConnection(); String sql = "select * from ordersdetail where userid = ? and productid = ?"; pstmt = connection.prepareStatement(sql); pstmt.setInt(1, orderid); pstmt.setString(2, productid); rs = pstmt.executeQuery(); if(rs.next()) { OrdersDetailmbz detailmbz = new OrdersDetailmbz(); detailmbz.setOrderidmbz(rs.getInt("orderid")); detailmbz.setProductidmbz(rs.getNString("productid")); detailmbz.setQuantitymbz(rs.getInt("quantity")); detailmbz.setUnitcostmbz(rs.getDouble("unitcost")); return detailmbz; } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { } } if (conn != null) { try { conn.close(); } catch (SQLException e) { } } } return null; } @Override public int create(OrdersDetailmbz ordersDetailmbz) { String sql = "insert into ordersdetail values(?,?,?,?)"; try ( Connection connection = DBHelper.getConnection(); PreparedStatement pstm = connection.prepareStatement(sql); ) { // 6.遍历结果集 pstm.setLong(1, ordersDetailmbz.getOrderidmbz()); pstm.setString(2, ordersDetailmbz.getProductidmbz()); pstm.setInt(3, ordersDetailmbz.getQuantitymbz()); pstm.setDouble(4, ordersDetailmbz.getUnitcostmbz()); int a = pstm.executeUpdate(); } catch (Exception e) { return -1; } return 0; } @Override public int delect(OrdersDetailmbz ordersDetailmbz) { String sql = "DELETE from ordersdetail where productid = ?"; try ( Connection connection = DBHelper.getConnection(); PreparedStatement pstm = connection.prepareStatement(sql)) { pstm.setString(1, ordersDetailmbz.getProductidmbz()); int affectedRows = pstm.executeUpdate(); System.out.printf("成功删除%d条数据。\n", affectedRows); } catch (Exception e) { e.printStackTrace(); } return 0; } // @Override // public int modify(OrdersDetailmbz ordersDetailmbz) { // return 0; // } }