Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 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. import homework.ToDoList;
  20.  
  21. @WebServlet("/History")
  22. public class TransactionHistory extends HttpServlet {
  23. private static final long serialVersionUID = 1L;
  24.  
  25. public TransactionHistory() {
  26. super();
  27. }
  28.  
  29. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  30. List<ItemModel> transactions = new ArrayList<ItemModel>();
  31. List<ItemModel> allTotalObj = new ArrayList<ItemModel>();
  32. Connection c = null;
  33. try
  34. {
  35. /* String url = "jdbc:mysql://localhost/roughdb";
  36. String username = "root";
  37. String password = "mypass";*/
  38.  
  39. String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
  40. String username = "cs3220stu38";
  41. String password = "w5woPb7Z";
  42.  
  43. c = DriverManager.getConnection( url, username, password );
  44. Statement stmt = c.createStatement();
  45. //ResultSet rs = stmt.executeQuery( "select * from shopping_cart" );
  46. ResultSet rs = stmt.executeQuery("SELECT name, SUM( each_total ) AS price, SUM( quantity ) AS quantity FROM shopping_cart GROUP BY name");
  47.  
  48.  
  49. while( rs.next() ){ //taking each row in the result
  50. ItemModel transaction = new ItemModel();
  51. //transaction.setId(rs.getInt("id"));
  52. transaction.setName(rs.getString("name"));
  53. transaction.setQuantity(rs.getInt("quantity"));
  54. transaction.setPrice(rs.getDouble("price"));
  55. //transaction.setEachTotal(rs.getDouble("each_total"));
  56. //transaction.setAllTotal(rs.getDouble("Price"));
  57. transactions.add(transaction);
  58. }
  59.  
  60. ResultSet rs2 = stmt.executeQuery("SELECT SUM(each_total) as each_total FROM shopping_cart");
  61. while(rs2.next()){
  62. ItemModel transaction2 = new ItemModel();
  63. transaction2.setAllTotal(rs2.getDouble("each_total"));
  64. allTotalObj.add(transaction2);
  65. }
  66.  
  67. }
  68. catch( SQLException e )
  69. {
  70. throw new ServletException( e );
  71. }
  72. finally
  73. {
  74. try
  75. {
  76. if( c != null ) c.close();
  77. }
  78. catch( SQLException e )
  79. {
  80. throw new ServletException( e );
  81. }
  82. }
  83. request.getSession().setAttribute("transactions", transactions);
  84. request.getSession().setAttribute("allTotalObj", allTotalObj);
  85. request.getRequestDispatcher("/WEB-INF/TransactionHistory.jsp").forward(request, response);
  86. }
  87.  
  88. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  89. // TODO Auto-generated method stub
  90. doGet(request, response);
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement