Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 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.annotation.WebServlet;
  16. import javax.servlet.http.HttpServlet;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19.  
  20. import homework.ToDoList;
  21.  
  22. @WebServlet("/Inventory")
  23. public class InventoryManager extends HttpServlet {
  24. private static final long serialVersionUID = 1L;
  25.  
  26. public InventoryManager() {
  27. super();
  28. }
  29.  
  30. public void init(ServletConfig config) throws ServletException {
  31. super.init(config);
  32.  
  33. try
  34. {
  35. Class.forName( "com.mysql.jdbc.Driver" );
  36. }
  37. catch( ClassNotFoundException e )
  38. {
  39. throw new ServletException( e );
  40. }
  41. }
  42.  
  43. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  44.  
  45. List<ItemModel> items = new ArrayList<ItemModel>();
  46. Connection c = null;
  47. try
  48. {
  49. /* String url = "jdbc:mysql://localhost/roughdb";
  50. String username = "root";
  51. String password = "mypass";*/
  52.  
  53. String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
  54. String username = "cs3220stu38";
  55. String password = "w5woPb7Z";
  56.  
  57.  
  58. c = DriverManager.getConnection( url, username, password );
  59.  
  60. Statement stmt = c.createStatement();
  61. ResultSet rs = stmt.executeQuery("select * from items");
  62.  
  63.  
  64. while( rs.next() ){ //taking each row in the result
  65. ItemModel item = new ItemModel();
  66. item.setId(rs.getInt("id"));
  67. item.setName(rs.getString("name"));
  68. item.setPrice(rs.getDouble("price"));
  69. item.setDetails(rs.getString("details"));
  70. item.setQuantity(rs.getInt("quantity"));
  71. items.add(item);
  72. }
  73.  
  74. }
  75. catch( SQLException e )
  76. {
  77. throw new ServletException( e );
  78. }
  79. finally
  80. {
  81. try
  82. {
  83. if( c != null ) c.close();
  84. }
  85. catch( SQLException e )
  86. {
  87. throw new ServletException( e );
  88. }
  89. }
  90. request.setAttribute("items", items);
  91. request.getRequestDispatcher("/WEB-INF/Inventory.jsp").forward(request, response);
  92. }
  93.  
  94. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  95. String name = request.getParameter("name");
  96. Double price = Double.parseDouble(request.getParameter("price"));
  97. int quantity = Integer.parseInt(request.getParameter("quantity"));
  98. String details = request.getParameter("details");
  99.  
  100. Connection c = null;
  101. try
  102. {
  103. /* String url = "jdbc:mysql://localhost/roughdb";
  104. String username = "root";
  105. String password = "mypass";*/
  106.  
  107. String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
  108. String username = "cs3220stu38";
  109. String password = "w5woPb7Z";
  110.  
  111. c = DriverManager.getConnection( url, username, password );
  112.  
  113. String sql = "insert into items (name, price, details, quantity) values (?,?,?,?)";
  114. PreparedStatement pstmt = c.prepareStatement(sql);
  115. pstmt.setString(1, name);
  116. pstmt.setDouble(2, price);
  117. pstmt.setString(3, details);
  118. pstmt.setInt(4, quantity);
  119. pstmt.executeUpdate();
  120.  
  121. }
  122. catch( SQLException e )
  123. {
  124. throw new ServletException( e );
  125. }
  126. finally
  127. {
  128. try
  129. {
  130. if( c != null ) c.close();
  131. }
  132. catch( SQLException e )
  133. {
  134. throw new ServletException( e );
  135. }
  136. }
  137. doGet(request, response);
  138. return;
  139. }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement