Advertisement
Guest User

Untitled

a guest
Nov 28th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.72 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.ServletException;
  14. import javax.servlet.annotation.WebServlet;
  15. import javax.servlet.http.HttpServlet;
  16. import javax.servlet.http.HttpServletRequest;
  17. import javax.servlet.http.HttpServletResponse;
  18.  
  19. @WebServlet("/ShoppingCart")
  20. public class ShoppingCart extends HttpServlet {
  21. private static final long serialVersionUID = 1L;
  22.  
  23. public ShoppingCart() {
  24. super();
  25. }
  26.  
  27. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  28.  
  29. //String cartItems = String(request.getAttribute("cartItems"));
  30. // Object cartItems = request.getServletContext().getAttribute("cartItems");
  31. // request.getServletContext().setAttribute("cartItems", cartItems);
  32. // System.out.println(cartItems);
  33. // request.getRequestDispatcher("/WEB-INF/ShoppingCart.jsp").forward(request, response);
  34.  
  35. Object cartItems = request.getAttribute("cartItems");
  36. request.setAttribute("cartItems", cartItems);
  37. System.out.println(cartItems);
  38. request.getRequestDispatcher("/WEB-INF/ShoppingCart.jsp").forward(request, response);
  39.  
  40. }
  41.  
  42. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  43. //List<ItemModel> cartItems = new ArrayList<ItemModel>();
  44.  
  45. Double price = Double.parseDouble(request.getParameter("price"));
  46. int quantity = Integer.valueOf(request.getParameter("quantity"));
  47. System.out.println(quantity);
  48. System.out.println(price);
  49. Integer id = Integer.valueOf(request.getParameter("id"));
  50. String name = request.getParameter("name");
  51. String details="";
  52. //cartItems.get(id).add(id, name, price, quantity,details);
  53. ItemModel cartItems = new ItemModel(id, name, price, quantity, details);
  54. System.out.println(cartItems.toString());
  55.  
  56. List<ItemModel> items = new ArrayList<ItemModel>();
  57. Connection c = null;
  58. try
  59. {
  60. String url = "jdbc:mysql://localhost/roughdb";
  61. String username = "root";
  62. String password = "mypass";
  63. /* String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
  64. String username = "cs3220stu38";
  65. String password = "w5woPb7Z"; */
  66.  
  67. c = DriverManager.getConnection( url, username, password );
  68.  
  69. //String sql = "insert into shopping_cart (name, quantity, price, each_total) values (?,?,?,?) where id = ?";
  70. String sql = "INSERT INTO shopping_cart(id, name, quantity, price, each_total) VALUES(?, ?, ?, ?, ?)";
  71.  
  72. PreparedStatement pstmt = c.prepareStatement(sql);
  73. pstmt.setInt(1, id);
  74. pstmt.setString(2, name);
  75. pstmt.setInt(3, quantity);
  76. pstmt.setDouble(4, price);
  77. pstmt.setDouble(5, quantity*price);
  78. pstmt.executeUpdate();
  79. }
  80. catch( SQLException e )
  81. {
  82. throw new ServletException( e );
  83. }
  84. finally
  85. {
  86. try
  87. {
  88. if( c != null ) c.close();
  89. }
  90. catch( SQLException e )
  91. {
  92. throw new ServletException( e );
  93. }
  94. }
  95.  
  96. //response.sendRedirect("Store");
  97. request.setAttribute("cartItems", cartItems);
  98. //request.getRequestDispatcher("/ShoppingCart.java").forward(request, response);
  99. response.sendRedirect("Store");
  100.  
  101. }
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement