Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. package assignment;
  2.  
  3. import java.io.IOException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12.  
  13. import javax.servlet.ServletConfig;
  14. import javax.servlet.ServletException;
  15. import javax.servlet.ServletRequest;
  16. import javax.servlet.annotation.WebServlet;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import javax.servlet.http.HttpSession;
  21. import javax.websocket.Session;
  22.  
  23. import assignment.ItemModel;
  24. import cs320.model.GuestBookEntry;;
  25.  
  26. @WebServlet("/ShoppingCart")
  27. public class ShoppingCart extends HttpServlet {
  28. private static final long serialVersionUID = 1L;
  29. int numberOfItems = 0;
  30. int totalPrice = 0;
  31. List<ItemModel> cartItems = new ArrayList<ItemModel>();
  32. public ShoppingCart() {
  33. super();
  34. }
  35.  
  36. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  37.  
  38.  
  39. //Object cartItems = request.getAttribute("cartItems");
  40. request.getSession().getAttribute("cartItems");
  41. //System.out.println(cartItems);
  42. request.getSession().getAttribute("numberOfItems");
  43. request.getRequestDispatcher("/WEB-INF/ShoppingCart.jsp").forward(request, response);
  44.  
  45. }
  46.  
  47. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  48. //List<ItemModel> cartItems = new ArrayList<ItemModel>();
  49. //List<ItemModel> cartItems = (List<ItemModel>) getServletContext().getAttribute("arrayList");
  50. //int numberOfItems = 0;
  51. Double price = Double.parseDouble(request.getParameter("price"));
  52. int quantity = Integer.valueOf(request.getParameter("quantity"));
  53. System.out.println(quantity);
  54. System.out.println(price);
  55. Integer id = Integer.valueOf(request.getParameter("id"));
  56. String name = request.getParameter("name");
  57. String details="";
  58. //cartItems.get(id).add(id, name, price, quantity,details);
  59. //ItemModel cart = new ItemModel(id, name, price, quantity, details);
  60. ItemModel cart = new ItemModel();
  61. cart.setId(id);
  62. cart.setName(name);
  63. cart.setPrice(price);
  64. cart.setQuantity(quantity);
  65. cart.setDetails(details);
  66. cartItems.add(cart);
  67.  
  68. numberOfItems++;
  69. //numberOfItems = cartItems.size();
  70. System.out.println("number of items in cart: "+numberOfItems);
  71.  
  72. //System.out.println(cartItems.toString());
  73.  
  74. List<ItemModel> items = new ArrayList<ItemModel>();
  75. Connection c = null;
  76. try
  77. {
  78. String url = "jdbc:mysql://localhost/roughdb";
  79. String username = "root";
  80. String password = "mypass";
  81. /* String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
  82. String username = "cs3220stu38";
  83. String password = "w5woPb7Z"; */
  84.  
  85.  
  86. c = DriverManager.getConnection( url, username, password );
  87.  
  88. //String sql = "insert into shopping_cart (name, quantity, price, each_total) values (?,?,?,?) where id = ?";
  89. String sql = "INSERT INTO shopping_cart(id, name, quantity, price, each_total) VALUES(?, ?, ?, ?, ?)";
  90.  
  91. PreparedStatement pstmt = c.prepareStatement(sql);
  92.  
  93. //int totalPrice = 0;
  94. totalPrice += quantity*price;
  95. System.out.println("total: "+totalPrice);
  96. pstmt.setInt(1, id);
  97. pstmt.setString(2, name);
  98. pstmt.setInt(3, quantity);
  99. pstmt.setDouble(4, price);
  100. pstmt.setDouble(5, quantity*price);
  101.  
  102. pstmt.executeUpdate();
  103. }
  104. catch( SQLException e )
  105. {
  106. throw new ServletException( e );
  107. }
  108. finally
  109. {
  110. try
  111. {
  112. if( c != null ) c.close();
  113. }
  114. catch( SQLException e )
  115. {
  116. throw new ServletException( e );
  117. }
  118. }
  119. request.getSession().setAttribute("numberOfItems", numberOfItems);
  120. //request.getSession().setAttribute("total", totalPrice);
  121. request.getSession().setAttribute("cartItems", cartItems);
  122. System.out.println("carItems: "+cartItems);
  123. doGet(request,response);
  124.  
  125. }
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement