Guest User

Untitled

a guest
Nov 28th, 2016
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. //List<ItemModel> cartItems = new ArrayList<ItemModel>();
  2.  
  3. Double price = Double.parseDouble(request.getParameter("price"));
  4. int quantity = Integer.valueOf(request.getParameter("quantity"));
  5. System.out.println(quantity);
  6. System.out.println(price);
  7. Integer id = Integer.valueOf(request.getParameter("id"));
  8. String name = request.getParameter("name");
  9. String details="";
  10. //cartItems.get(id).add(id, name, price, quantity,details);
  11. ItemModel cartItems = new ItemModel(id, name, price, quantity, details);
  12. System.out.println(cartItems.toString());
  13.  
  14. List<ItemModel> items = new ArrayList<ItemModel>();
  15. Connection c = null;
  16. try
  17. {
  18. String url = "jdbc:mysql://localhost/roughdb";
  19. String username = "root";
  20. String password = "mypass";
  21. /* String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
  22. String username = "cs3220stu38";
  23. String password = "w5woPb7Z"; */
  24.  
  25. c = DriverManager.getConnection( url, username, password );
  26.  
  27. //String sql = "insert into shopping_cart (name, quantity, price, each_total) values (?,?,?,?) where id = ?";
  28. String sql = "INSERT INTO shopping_cart(id, name, quantity, price, each_total) VALUES(?, ?, ?, ?, ?)";
  29.  
  30. PreparedStatement pstmt = c.prepareStatement(sql);
  31. pstmt.setInt(1, id);
  32. pstmt.setString(2, name);
  33. pstmt.setInt(3, quantity);
  34. pstmt.setDouble(4, price);
  35. pstmt.setDouble(5, quantity*price);
  36. pstmt.executeUpdate();
  37. }
  38. catch( SQLException e )
  39. {
  40. throw new ServletException( e );
  41. }
  42. finally
  43. {
  44. try
  45. {
  46. if( c != null ) c.close();
  47. }
  48. catch( SQLException e )
  49. {
  50. throw new ServletException( e );
  51. }
  52. }
  53.  
  54. //response.sendRedirect("Store");
  55. request.getServletContext().setAttribute("cartItems", cartItems);
  56. //request.getRequestDispatcher("/ShoppingCart.java").forward(request, response);
  57. response.sendRedirect("ShoppingCart");
  58. //return;
  59.  
  60. //request.getRequestDispatcher("/WEB-INF/ShoppingCart.jsp").forward(request, response);
  61.  
  62. //request.getRequestDispatcher("/WEB-INF/StoreFront.jsp").forward(request, response);
Add Comment
Please, Sign In to add comment