Guest User

Untitled

a guest
Aug 12th, 2018
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 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 com.mysql.jdbc.Connection;
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.sql.DriverManager;
  11. import java.sql.PreparedStatement;
  12. import java.sql.SQLException;
  13. import java.util.logging.Level;
  14. import java.util.logging.Logger;
  15. import javax.servlet.ServletException;
  16. import javax.servlet.http.Cookie;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20. import javax.servlet.http.HttpSession;
  21.  
  22. /**
  23. *
  24. * @author Prasanth
  25. */
  26. public class update extends HttpServlet {
  27.  
  28. /**
  29. * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  30. * methods.
  31. *
  32. * @param request servlet request
  33. * @param response servlet response
  34. * @throws ServletException if a servlet-specific error occurs
  35. * @throws IOException if an I/O error occurs
  36. */
  37. protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  38. throws ServletException, IOException, ClassNotFoundException, SQLException {
  39. response.setContentType("text/html;charset=UTF-8");
  40. Class.forName("com.mysql.jdbc.Driver");
  41. System.out.println("Connecting to database......");
  42. String h=Insert.uu;
  43. Connection con=(Connection) DriverManager.getConnection("jdbc:mysql://localhost/j2ee","root","root");
  44. System.out.println("Connected to Database");
  45.  
  46. try (PrintWriter out = response.getWriter()) {
  47. /* TODO output your page here. You may use following sample code. */
  48. Cookie ck[]=request.getCookies();
  49. String u=ck[0].getValue();
  50. //System.out.println(u);
  51. String p = request.getParameter("pass");
  52. String n=request.getParameter("name");
  53. String a = request.getParameter("ag");
  54. int age=Integer.parseInt(a);
  55.  
  56.  
  57. HttpSession session=request.getSession(false);
  58. String uidd=(String)session.getAttribute("uname");
  59.  
  60. String sql="update userinfo set pass=?,name=?,age=? where uid=?";
  61. PreparedStatement stmnt;
  62. stmnt=con.prepareStatement(sql);
  63. stmnt.setString(4,u);
  64. stmnt.setString(1,p);
  65. stmnt.setString(2,n);
  66. stmnt.setInt(3, age);
  67. stmnt.executeUpdate();
  68.  
  69. out.println("<!DOCTYPE html>");
  70. out.println("<html>");
  71. out.println("<head>");
  72. out.println("<title>Servlet update</title>");
  73. out.println("</head>");
  74. out.println("<body>");
  75. out.println("<h1>Successs </h1>" );
  76. out.println("</body>");
  77. out.println("</html>");
  78. }
  79. }
  80.  
  81. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  82. /**
  83. * Handles the HTTP <code>GET</code> method.
  84. *
  85. * @param request servlet request
  86. * @param response servlet response
  87. * @throws ServletException if a servlet-specific error occurs
  88. * @throws IOException if an I/O error occurs
  89. */
  90. @Override
  91. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  92. throws ServletException, IOException {
  93. try {
  94. processRequest(request, response);
  95. } catch (ClassNotFoundException ex) {
  96. Logger.getLogger(update.class.getName()).log(Level.SEVERE, null, ex);
  97. } catch (SQLException ex) {
  98. Logger.getLogger(update.class.getName()).log(Level.SEVERE, null, ex);
  99. }
  100. }
  101.  
  102. /**
  103. * Handles the HTTP <code>POST</code> method.
  104. *
  105. * @param request servlet request
  106. * @param response servlet response
  107. * @throws ServletException if a servlet-specific error occurs
  108. * @throws IOException if an I/O error occurs
  109. */
  110. @Override
  111. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  112. throws ServletException, IOException {
  113. try {
  114. processRequest(request, response);
  115. } catch (ClassNotFoundException ex) {
  116. Logger.getLogger(update.class.getName()).log(Level.SEVERE, null, ex);
  117. } catch (SQLException ex) {
  118. Logger.getLogger(update.class.getName()).log(Level.SEVERE, null, ex);
  119. }
  120. }
  121.  
  122. /**
  123. * Returns a short description of the servlet.
  124. *
  125. * @return a String containing servlet description
  126. */
  127. @Override
  128. public String getServletInfo() {
  129. return "Short description";
  130. }// </editor-fold>
  131.  
  132. }
Add Comment
Please, Sign In to add comment