Advertisement
Guest User

Untitled

a guest
Aug 14th, 2016
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. public ShoppingCart getShoppingCart(Account acc){
  2. ShoppingCart cart = null;
  3. ArrayList<Product> prodList = new ArrayList<Product>();
  4. int userID;
  5. DBConnector connector = new DBConnector();
  6. Connection conn = connector.getConnection();
  7. PreparedStatement pstmt;
  8. String sql = "SELECT id FROM customer_account WHERE username = ? AND password = ?";
  9. try{
  10. pstmt = conn.prepareStatement(sql);
  11. pstmt.setString(1, acc.getUsername());
  12. pstmt.setString(2, acc.getPassword());
  13. ResultSet rs = pstmt.executeQuery();
  14. userID = rs.getInt("id");
  15. rs.close();
  16. pstmt.close();
  17. sql = "SELECT product_id, quantity FROM shopping_cart WHERE user_id = ?";
  18. pstmt = conn.prepareStatement(sql);
  19. pstmt.setInt(1, userID);
  20. rs = pstmt.executeQuery();
  21. pstmt.close();
  22. while(rs.next()){
  23. int prodID = rs.getInt("product_id");
  24. int qty = rs.getInt("quantity");
  25. pstmt = conn.prepareStatement("SELECT * FROM product");
  26. Product prod = new Product(rs.getInt("id"), rs.getString("name"), rs.getString("description"), rs.getDouble("price"),
  27. rs.getString("category"), rs.getString("image"));
  28. for(int i = 0; i < qty; i++){
  29. prodList.add(prod);
  30. }
  31. pstmt.close();
  32. }
  33. //pstmt.close();
  34. cart = new ShoppingCart(userID, prodList);
  35. conn.close();
  36. }catch (SQLException e) {
  37. // TODO Auto-generated catch block
  38. e.printStackTrace();
  39. }
  40.  
  41. return cart;
  42.  
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement