Guest User

Untitled

a guest
Nov 28th, 2016
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.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 assignment.ItemModel;;
  21.  
  22. @WebServlet(urlPatterns = "/Store")
  23. public class StoreFront extends HttpServlet {
  24. private static final long serialVersionUID = 1L;
  25.  
  26. public StoreFront() {
  27. super();
  28. }
  29.  
  30. public void init(ServletConfig config) throws ServletException {
  31. super.init(config);
  32.  
  33. try {
  34. Class.forName("com.mysql.jdbc.Driver");
  35. } catch (ClassNotFoundException e) {
  36. throw new ServletException(e);
  37. }
  38. }
  39.  
  40. protected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
  41. List<ItemModel> items = new ArrayList<ItemModel>();
  42. Connection c = null;
  43. try {
  44. String url = "jdbc:mysql://localhost/roughdb";
  45. String username = "root";
  46. String password = "mypass";
  47.  
  48. /* String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
  49. String username = "cs3220stu38";
  50. String password = "w5woPb7Z";*/
  51.  
  52. c = DriverManager.getConnection(url, username, password);
  53. Statement stmt = c.createStatement();
  54. ResultSet rs = stmt.executeQuery("select * from items where quantity>0");
  55.  
  56. while (rs.next()) {
  57. ItemModel item = new ItemModel(rs.getInt("id"), rs.getString("name"), rs.getDouble("price"), rs.getInt("quantity")
  58. ,rs.getString("details"));
  59. items.add(item);
  60. }
  61.  
  62.  
  63.  
  64. } catch (SQLException e) {
  65. throw new ServletException(e);
  66. } finally {
  67. try {
  68. if (c != null)
  69. c.close();
  70. } catch (SQLException e) {
  71. throw new ServletException(e);
  72. }
  73. }
  74. request.setAttribute("items", items);
  75. request.getRequestDispatcher("/WEB-INF/StoreFront.jsp").forward(request, response);
  76.  
  77. }
  78.  
  79. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  80. doGet(request, response);
  81. }
  82.  
  83. }
Add Comment
Please, Sign In to add comment