CartItemDAO
unknown
plain_text
5 months ago
1.8 kB
3
Indexable
package DAO; import Models.CartItem; import java.sql.*; import java.util.ArrayList; import java.util.List; public class CartItemDAO { // Phương thức lấy CartItem theo userId public List<CartItem> getCartItemsByUserId(int userId) { List<CartItem> cartItems = new ArrayList<>(); String sql = "SELECT ci.cart_item_id, ci.user_id, ci.product_id, p.product_name, ci.quantity, p.price " + "FROM CartItem ci " + "JOIN Product p ON ci.product_id = p.product_id " + "WHERE ci.user_id = ?"; try (Connection conn = getConnection(); // Thay thế bằng phương thức kết nối của bạn PreparedStatement ps = conn.prepareStatement(sql)) { ps.setInt(1, userId); try (ResultSet rs = ps.executeQuery()) { while (rs.next()) { CartItem item = new CartItem(); item.setCart_item_id(rs.getInt("cart_item_id")); item.setUser_id(rs.getInt("user_id")); item.setProduct_id(rs.getInt("product_id")); item.setProduct_name(rs.getString("product_name")); // Lấy tên sản phẩm item.setQuantity(rs.getInt("quantity")); item.setProduct_price(rs.getDouble("price")); // Lấy giá sản phẩm cartItems.add(item); } } } catch (SQLException e) { e.printStackTrace(); } return cartItems; } // Phương thức để kết nối tới cơ sở dữ liệu private Connection getConnection() { // Thay thế bằng cách kết nối cơ sở dữ liệu của bạn return null; } }
Editor is loading...
Leave a Comment