Advertisement
Guest User

UpdateLarare.java (SERVLET)

a guest
Feb 8th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.SQLException;
  12. import java.sql.Statement;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.servlet.RequestDispatcher;
  16. import javax.servlet.ServletException;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20.  
  21. /**
  22. *
  23. * @author sofiehamilton
  24. */
  25.  
  26. public class UpdateLarare extends HttpServlet {
  27.  
  28. Connection conn;
  29. Statement stmt;
  30. String firstname;
  31. String lastname;
  32. String email;
  33. int phone;
  34. String password;
  35. String admin = "0";
  36. String dburl = "jdbc:mysql://localhost:3306/oru?zeroDateTimeBehavior=convertToNull";
  37. String Username = "root";
  38. String PassWord = "";
  39.  
  40. /**
  41. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  42. * methods.
  43. *
  44. * @param request servlet request
  45. * @param response servlet response
  46. * @throws ServletException if a servlet-specific error occurs
  47. * @throws IOException if an I/O error occurs
  48. */
  49. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  50. throws ServletException, IOException, ClassNotFoundException, SQLException {
  51. response.setContentType("text/html;charset=UTF-8");
  52. try (PrintWriter out = response.getWriter()) {
  53.  
  54. firstname = request.getParameter("fornamntxt");
  55. lastname = request.getParameter("efternamntxt");
  56. email = request.getParameter("emailtxt");
  57. password = request.getParameter("passwordtxt");
  58. phone = Integer.parseInt(request.getParameter("nummertxt"));
  59.  
  60. admin = request.getParameter("adminbtn");
  61. if (admin != null)
  62. admin = "1";
  63.  
  64.  
  65. Class.forName("com.mysql.jdbc.Driver");
  66. conn = DriverManager.getConnection(dburl, Username, PassWord);
  67. stmt = conn.createStatement();
  68.  
  69. // Execute SQL query
  70. String query = "update LARARE set "
  71. + "PHONE = '" + phone + "' , "
  72. + "EMAIL = '" + email + "' ,"
  73. + "PASSWORD = '" + password + "',"
  74. + "ADMIN = '" + admin + "'"
  75. + " where FIRSTNAME = '" + firstname + "' and LASTNAME = '" + lastname + "'";
  76.  
  77. stmt.executeQuery(query);
  78.  
  79.  
  80. }
  81.  
  82. catch (Exception e){
  83. e.printStackTrace();
  84. }
  85. }
  86.  
  87. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  88. /**
  89. * Handles the HTTP <code>GET</code> method.
  90. *
  91. * @param request servlet request
  92. * @param response servlet response
  93. * @throws ServletException if a servlet-specific error occurs
  94. * @throws IOException if an I/O error occurs
  95. */
  96. @Override
  97. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  98. throws ServletException, IOException {
  99. try {
  100. processRequest(request, response);
  101. } catch (ClassNotFoundException ex) {
  102. Logger.getLogger(UpdateLarare.class.getName()).log(Level.SEVERE, null, ex);
  103. } catch (SQLException ex) {
  104. Logger.getLogger(UpdateLarare.class.getName()).log(Level.SEVERE, null, ex);
  105. }
  106. }
  107.  
  108. /**
  109. * Handles the HTTP <code>POST</code> method.
  110. *
  111. * @param request servlet request
  112. * @param response servlet response
  113. * @throws ServletException if a servlet-specific error occurs
  114. * @throws IOException if an I/O error occurs
  115. */
  116. @Override
  117. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  118. throws ServletException, IOException {
  119. try {
  120. processRequest(request, response);
  121. } catch (ClassNotFoundException ex) {
  122. Logger.getLogger(UpdateLarare.class.getName()).log(Level.SEVERE, null, ex);
  123. } catch (SQLException ex) {
  124. Logger.getLogger(UpdateLarare.class.getName()).log(Level.SEVERE, null, ex);
  125. }
  126. }
  127.  
  128. /**
  129. * Returns a short description of the servlet.
  130. *
  131. * @return a String containing servlet description
  132. */
  133. @Override
  134. public String getServletInfo() {
  135. return "Short description";
  136. }// </editor-fold>
  137.  
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement