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.
java/OrderDetailDaoImpl.java

161 lines
4.8 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 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<OrderDetail> findAll() {
//通过执行SQL语句查询所有订单详细信息并将结果封装成OrderDetail对象的列表返回。
String sql = "select * from account";
List<OrderDetail> list = new ArrayList<OrderDetail>();
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;
}
}