package jpestore.dao.mysql; 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 jpestore.dao.OrdertailDao; import jpestore.domain.OrderDetail; import jpestore.domain.Product; public class OrderDetailDaolmp implements OrdertailDao { @Override public List findAll() { // TODO 自动生成的方法存根 return null; } @Override public OrderDetail findByPl(int orderid, String productid) { Connection conn = null; PreparedStatement pstmt = null ; ResultSet rs = null; Listlist = new ArrayList(); String sql = "select orderid,productid,quantity,unitcost from ordersdetail where orderid=? and productid=?"; try { pstmt = conn.prepareStatement(sql); pstmt.setInt(1, orderid); pstmt.setString(2, productid); rs = pstmt.executeQuery(); if(rs.next()) { OrderDetail detail = new OrderDetail(); detail.setOrderid(rs.getInt("orderid")); detail.setProductid(rs.getString("orderid")); detail.setQuantity(rs.getInt("quantity")); detail.setUnitcost(rs.getDouble("unitcost")); }} catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); }finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if (pstmt != null) { try { pstmt.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } // TODO 自动生成的方法存根 return null ; } @Override public int create(OrderDetail orderDetail) { String sql = "insert into ordersdetail (orderid,productid,quantity,unitcost)values (?,?,?,?)"; try( Connection conn = DBHelper.getConnection(); PreparedStatement pstmt = conn.prepareStatement(sql); ){pstmt.setLong(1, orderDetail.getOrderid()); pstmt.setString(2, orderDetail.getProductid()); pstmt.setInt(3,orderDetail.getQuantity()); pstmt.setDouble(4, orderDetail.getUnitcost()); 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) { // TODO Auto-generated method stub String sql = "update ordersdetail set unitcost = ? where orderid = ? and productid = ?"; try ( // 2.创建数据库连接 Connection conn = DBHelper.getConnection(); // 3. 创建语句对象 PreparedStatement pstmt= conn.prepareStatement(sql) ) { // 4. 绑定参数 pstmt.setDouble(1, orderDetail.getUnitcost()); 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) { // TODO Auto-generated method stub String sql = "delete from ordersdetail where orderid = ? and productid = ?"; try ( // 2.创建数据库连接 Connection conn = DBHelper.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; } }