Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public ShoppingCart getShoppingCart(Account acc){
- ShoppingCart cart = null;
- ArrayList<Product> prodList = new ArrayList<Product>();
- int userID;
- DBConnector connector = new DBConnector();
- Connection conn = connector.getConnection();
- PreparedStatement pstmt;
- String sql = "SELECT id FROM customer_account WHERE username = ? AND password = ?";
- try{
- pstmt = conn.prepareStatement(sql);
- pstmt.setString(1, acc.getUsername());
- pstmt.setString(2, acc.getPassword());
- ResultSet rs = pstmt.executeQuery();
- userID = rs.getInt("id");
- rs.close();
- pstmt.close();
- sql = "SELECT product_id, quantity FROM shopping_cart WHERE user_id = ?";
- pstmt = conn.prepareStatement(sql);
- pstmt.setInt(1, userID);
- rs = pstmt.executeQuery();
- pstmt.close();
- while(rs.next()){
- int prodID = rs.getInt("product_id");
- int qty = rs.getInt("quantity");
- pstmt = conn.prepareStatement("SELECT * FROM product");
- Product prod = new Product(rs.getInt("id"), rs.getString("name"), rs.getString("description"), rs.getDouble("price"),
- rs.getString("category"), rs.getString("image"));
- for(int i = 0; i < qty; i++){
- prodList.add(prod);
- }
- pstmt.close();
- }
- //pstmt.close();
- cart = new ShoppingCart(userID, prodList);
- conn.close();
- }catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return cart;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement