Advertisement
Guest User

Untitled

a guest
Dec 18th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. public void UpdateUserDATA(User user) throws SQLException {
  2. PreparedStatement preparedStatement =null;
  3. try {
  4. Connection connection = getConnection();
  5. preparedStatement = connection.prepareStatement("update userregisteration set firstName=?, lastName=?, username=?,password=?, telephone=?, address=? where username=? ");
  6. preparedStatement.setString(1, user.getFirst_name());
  7. preparedStatement.setString(2, user.getLast_name());
  8. preparedStatement.setString(3, user.getUsername());
  9. preparedStatement.setString(4, user.getPassword());
  10. preparedStatement.setString(5, user.getContactNo());
  11. preparedStatement.setString(6, user.getAddress());
  12. preparedStatement.setString(7, user.getUsername());
  13. preparedStatement.executeUpdate();
  14. } catch (SQLException e) {
  15. e.printStackTrace();
  16. } finally {
  17.  
  18. if (preparedStatement != null) {
  19. preparedStatement.close();
  20. }
  21. }
  22.  
  23. }
  24.  
  25. package Controller;
  26.  
  27. import DatabaseConnector.DatabaseConnectivity;
  28. import DatabaseConnector.UserDataConnector;
  29. import java.io.IOException;
  30. import java.sql.SQLException;
  31. import java.util.logging.Level;
  32. import java.util.logging.Logger;
  33. import javax.servlet.ServletException;
  34. import javax.servlet.http.HttpServlet;
  35. import javax.servlet.http.HttpServletRequest;
  36. import javax.servlet.http.HttpServletResponse;
  37. import javax.servlet.http.HttpSession;
  38. import model.User;
  39.  
  40.  
  41. /**
  42. *
  43. * @author Thanuka_Senadheera
  44. */
  45. public class UserUpdateServlet extends HttpServlet {
  46.  
  47.  
  48. private final DatabaseConnectivity DBConnection = new DatabaseConnectivity();
  49.  
  50.  
  51.  
  52. public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException
  53. {
  54.  
  55. resp.setContentType("text/html;charset=UTF-8");
  56.  
  57.  
  58. if (!DBConnection.ConnectionValid())
  59. {
  60. resp.sendRedirect("404/404.html"); //Connection to the database was not successful
  61. }
  62. else
  63. {
  64. HttpSession session = req.getSession();
  65. String username = (String) session.getAttribute("username");
  66. String password = (String) session.getAttribute("password");
  67. if (DBConnection.UserValidation(username, password))
  68. {
  69. UserDataConnector UDC = new UserDataConnector();
  70. User user = (User) session.getAttribute("user");
  71. try {
  72. UDC.UpdateUserDATA(user);
  73. } catch (SQLException ex) {
  74. Logger.getLogger(UserUpdateServlet.class.getName()).log(Level.SEVERE, null, ex);
  75. }
  76. session.setAttribute("user",user);
  77. req.setAttribute("message","Your details are successfully updated, Please Go to home page!");
  78. resp.sendRedirect("manageAccounts.jsp");
  79. }
  80. else
  81. {
  82. req.setAttribute("message","Account is invalid, Please SignUp!");
  83. req.getRequestDispatcher("Login.jsp").forward(req,resp);
  84. }
  85. }
  86. }
  87.  
  88.  
  89. }
  90.  
  91. package model;
  92.  
  93. import java.io.Serializable;
  94.  
  95. /**
  96. *
  97. * @author Thanuka_Senadheera
  98. */
  99. public class User implements Serializable
  100. {
  101. private String first_name;
  102. private String last_name;
  103. private String username;
  104. private String password;
  105. private String contactNo;
  106. private String address;
  107.  
  108. public User(String first_name,String last_name,String username,String password,String contactNo,String address)
  109. {
  110. this.first_name=first_name;
  111. this.last_name=last_name;
  112. this.username=username;
  113. this.password=password;
  114. this.contactNo=contactNo;
  115. this.address=address;
  116.  
  117. }
  118.  
  119. public User()
  120. {
  121.  
  122. }
  123.  
  124. public void setFirst_name(String first_name) {
  125. this.first_name = first_name;
  126. }
  127.  
  128. public void setLast_name(String last_name) {
  129. this.last_name = last_name;
  130. }
  131.  
  132. public void setUsername(String username) {
  133. this.username = username;
  134. }
  135.  
  136. public void setPassword(String password) {
  137. this.password = password;
  138. }
  139.  
  140. public void setContactNo(String contactNo) {
  141. this.contactNo = contactNo;
  142. }
  143.  
  144. public void setAddress(String address) {
  145. this.address = address;
  146. }
  147.  
  148. public String getFirst_name() {
  149. return first_name;
  150. }
  151.  
  152. public String getLast_name() {
  153. return last_name;
  154. }
  155.  
  156. public String getUsername() {
  157. return username;
  158. }
  159.  
  160. public String getPassword() {
  161. return password;
  162. }
  163.  
  164. public String getContactNo() {
  165. return contactNo;
  166. }
  167.  
  168. public String getAddress() {
  169. return address;
  170. }
  171.  
  172.  
  173. /*
  174. @Override
  175. public String toString() {
  176. return "User [first_name=" + first_name + ", last_name=" + last_name
  177. + ", username=" + username + ", password=" + password + ", contactNo="
  178. + contactNo + ", address=" + address + "]";
  179. }
  180. */
  181.  
  182. }
  183.  
  184.  
  185. ----------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement