Guest User

Untitled

a guest
Nov 29th, 2016
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 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://cs3.calstatela.edu/cs3220stu49";
  45. String username = "cs3220stu49";
  46. String password = "#CKr.Npn";
  47.  
  48.  
  49. c = DriverManager.getConnection(url, username, password);
  50. Statement stmt = c.createStatement();
  51. ResultSet rs = stmt.executeQuery("select * from items where quantity>0");
  52.  
  53. while (rs.next()) {
  54. ItemModel item = new ItemModel(rs.getInt("id"), rs.getString("name"), rs.getDouble("price"), rs.getInt("quantity")
  55. ,rs.getString("details"));
  56. items.add(item);
  57. }
  58.  
  59.  
  60.  
  61. } catch (SQLException e) {
  62. throw new ServletException(e);
  63. } finally {
  64. try {
  65. if (c != null)
  66. c.close();
  67. } catch (SQLException e) {
  68. throw new ServletException(e);
  69. }
  70. }
  71. request.getSession().setAttribute("items", items);
  72. request.getRequestDispatcher("/WEB-INF/StoreFront.jsp").forward(request, response);
  73.  
  74. }
  75.  
  76. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  77. doGet(request, response);
  78. }
  79.  
  80. }
Add Comment
Please, Sign In to add comment