You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Hotel_service_system/OrderDetailDaolmp.java

143 lines
4.2 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

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<OrderDetail> findAll() {
// TODO 自动生成的方法存根
return null;
}
@Override
public OrderDetail findByPl(int orderid, String productid) {
Connection conn = null;
PreparedStatement pstmt = null ;
ResultSet rs = null;
List<Product>list = new ArrayList<Product>();
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;
}
}