Advertisement
Guest User

Untitled

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