Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 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.SQLException;
  8.  
  9. import javax.servlet.ServletException;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. @WebServlet("/RemoveItem")
  16. public class RemoveItem extends HttpServlet {
  17. private static final long serialVersionUID = 1L;
  18.  
  19. public RemoveItem() {
  20. super();
  21. }
  22.  
  23. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  24. Integer id = Integer.valueOf(request.getParameter("id"));
  25.  
  26. Connection c = null;
  27. try
  28. {
  29. /* String url = "jdbc:mysql://localhost/roughdb";
  30. String username = "root";
  31. String password = "mypass";*/
  32.  
  33. String url = "jdbc:mysql://cs3.calstatela.edu/cs3220stu38";
  34. String username = "cs3220stu38";
  35. String password = "w5woPb7Z";
  36.  
  37.  
  38. String sql = "DELETE FROM items WHERE id = ?";
  39. c = DriverManager.getConnection( url, username, password );
  40.  
  41. PreparedStatement pstmt = c.prepareStatement(sql);
  42. pstmt.setInt(1, id);
  43. pstmt.executeUpdate();
  44.  
  45. }
  46.  
  47. catch( SQLException e )
  48. {
  49. throw new ServletException( e );
  50. }
  51. finally
  52. {
  53. try
  54. {
  55. if( c != null ) c.close();
  56. }
  57. catch( SQLException e )
  58. {
  59. throw new ServletException( e );
  60. }
  61. }
  62. response.sendRedirect("Inventory");
  63. }
  64.  
  65. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  66. doGet(request, response);
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement