Advertisement
Guest User

Untitled

a guest
Nov 4th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 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. package dataaccess;
  7.  
  8. import business.User;
  9. import java.io.*;
  10. import java.sql.Connection;
  11. import java.sql.PreparedStatement;
  12. import java.sql.SQLException;
  13. import java.sql.DriverManager;
  14. import java.sql.ResultSet;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. import java.util.Set;
  18. import java.util.logging.Level;
  19. import java.util.logging.Logger;
  20.  
  21. public class UserDB {
  22.  
  23. public static int insert(User user) throws IOException, SQLException, ClassNotFoundException {
  24. // load the driver
  25. ConnectionPool pool = ConnectionPool.getInstance();
  26. Connection connection = pool.getConnection();
  27.  
  28. PreparedStatement ps = null;
  29.  
  30. String query = "INSERT INTO user (fullname, username, emailAddress, password, birthdate, questionNo, answer) "
  31. + "VALUES (?, ?, ?, ?, ?, ?, ?)";
  32.  
  33. try {
  34. ps = connection.prepareStatement(query);
  35. ps.setString(1, user.getFullName());
  36. ps.setString(2, user.getUserName());
  37. ps.setString(3, user.getEmail());
  38. ps.setString(4, user.getPassword());
  39. ps.setString(5, user.getBirthdate());
  40. ps.setString(6, user.getQuestionNo());
  41. ps.setString(7, user.getAnswer());
  42. return ps.executeUpdate();
  43. } catch (SQLException e) {
  44. System.out.println(e);
  45. return 0;
  46. } finally {
  47. DBUtil.closePreparedStatement(ps);
  48. pool.freeConnection(connection);
  49. }
  50.  
  51. }
  52.  
  53. public static int updateUser(User user) throws IOException, SQLException, ClassNotFoundException {
  54. ConnectionPool pool = ConnectionPool.getInstance();
  55. Connection connection = pool.getConnection();
  56.  
  57. PreparedStatement ps = null;
  58.  
  59. String query = "Update user set fullname = ?, birthdate = ?, password = ?, questionNo = ?, answer = ? where emailAddress = ?";
  60.  
  61. int result = 0;
  62.  
  63. try {
  64. ps = connection.prepareStatement(query);
  65. ps.setString(1, user.getFullName());
  66. ps.setString(2, user.getBirthdate());
  67. ps.setString(3, user.getPassword());
  68. ps.setString(4, user.getQuestionNo());
  69. ps.setString(5, user.getAnswer());
  70. ps.setString(6, user.getEmail());
  71.  
  72. result = ps.executeUpdate();
  73. } catch (SQLException e) {
  74. System.out.println(e);
  75. } finally {
  76. DBUtil.closePreparedStatement(ps);
  77. pool.freeConnection(connection);
  78. }
  79.  
  80. return result;
  81. }
  82.  
  83. public static int updatePassword(String emailAddress, String Password) throws SQLException, ClassNotFoundException {
  84. ConnectionPool pool = ConnectionPool.getInstance();
  85. Connection connection = pool.getConnection();
  86.  
  87. PreparedStatement ps = null;
  88.  
  89. String query = "Update user set password = ? where emailAddress= ?";
  90.  
  91. int result = 0;
  92.  
  93. try {
  94. ps = connection.prepareStatement(query);
  95. ps.setString(1, Password);
  96. ps.setString(2, emailAddress);
  97.  
  98. result = ps.executeUpdate();
  99.  
  100. } catch (SQLException e) {
  101. System.out.println(e);
  102. return 0;
  103. } finally {
  104. DBUtil.closePreparedStatement(ps);
  105. pool.freeConnection(connection);
  106. }
  107. return result;
  108. }
  109.  
  110. public static User search(String emailAddress) {
  111. ConnectionPool pool = ConnectionPool.getInstance();
  112. Connection connection = pool.getConnection();
  113.  
  114. PreparedStatement ps = null;
  115. ResultSet rs = null;
  116.  
  117. String query = "SELECT * FROM User "
  118. + "WHERE emailAddress = ?";
  119.  
  120. try {
  121. ps = connection.prepareStatement(query);
  122. ps.setString(1, emailAddress);
  123. rs = ps.executeQuery();
  124.  
  125. User user = null;
  126. if (rs.next()) {
  127. user = new User();
  128. user.setFullName(rs.getString("fullName"));
  129. user.setUserName(rs.getString("userName"));
  130. user.setEmail(rs.getString("emailAddress"));
  131. user.setPassword(rs.getString("password"));
  132. user.setBirthdate(rs.getString("birthdate"));
  133. user.setQuestionNo(rs.getString("questionNo"));
  134. user.setAnswer(rs.getString("answer"));
  135. }
  136. return user;
  137. } catch (SQLException e) {
  138. System.out.println(e);
  139. return null;
  140. } finally {
  141. DBUtil.closePreparedStatement(ps);
  142. pool.freeConnection(connection);
  143. }
  144.  
  145. }
  146.  
  147. public static boolean emailExists(String email) {
  148. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  149. }
  150.  
  151. public static User select(String emailAddress) {
  152. ConnectionPool pool = ConnectionPool.getInstance();
  153. Connection connection = pool.getConnection();
  154.  
  155. PreparedStatement ps = null;
  156. ResultSet rs = null;
  157.  
  158. String query = "SELECT * FROM User "
  159. + "WHERE emailAddress = ?";
  160.  
  161. try {
  162. ps = connection.prepareStatement(query);
  163. ps.setString(1, emailAddress);
  164. rs = ps.executeQuery();
  165.  
  166. User user = null;
  167. if (rs.next()) {
  168. user = new User();
  169. user.setFullName(rs.getString("fullName"));
  170. user.setUserName(rs.getString("userName"));
  171. user.setEmail(rs.getString("emailAddress"));
  172. user.setPassword(rs.getString("password"));
  173. user.setBirthdate(rs.getString("birthdate"));
  174. user.setQuestionNo(rs.getString("questionNo"));
  175. user.setAnswer(rs.getString("answer"));
  176. }
  177. return user;
  178. } catch (SQLException e) {
  179. System.out.println(e);
  180. return null;
  181. }finally {
  182. DBUtil.closePreparedStatement(ps);
  183. pool.freeConnection(connection);
  184. }
  185. }
  186.  
  187. public static User searchUserName(String emailAddress) {
  188. ConnectionPool pool = ConnectionPool.getInstance();
  189. Connection connection = pool.getConnection();
  190.  
  191. PreparedStatement ps = null;
  192. ResultSet rs = null;
  193.  
  194. String query = "SELECT * FROM User "
  195. + "WHERE username = ?";
  196.  
  197. try {
  198. ps = connection.prepareStatement(query);
  199. ps.setString(1, emailAddress);
  200. rs = ps.executeQuery();
  201.  
  202. User user = null;
  203. if (rs.next()) {
  204. user = new User();
  205. user.setFullName(rs.getString("fullName"));
  206. user.setUserName(rs.getString("userName"));
  207. user.setEmail(rs.getString("emailAddress"));
  208. user.setPassword(rs.getString("password"));
  209. user.setBirthdate(rs.getString("birthdate"));
  210. user.setQuestionNo(rs.getString("questionNo"));
  211. user.setAnswer(rs.getString("answer"));
  212. }
  213. return user;
  214. } catch (SQLException e) {
  215. System.out.println(e);
  216. return null;
  217. }finally {
  218. DBUtil.closePreparedStatement(ps);
  219. pool.freeConnection(connection);
  220. }
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement