Advertisement
eventhelawn

Untitled

Nov 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. private static final Logger l = Logger.getLogger(UserDAO.class);
  2.  
  3. public void register(User user) throws SQLException {
  4. Connection c = DBConnectionManager.INSTANCE.getConnection();
  5. l.trace("Connection established");
  6. c.setAutoCommit(false);
  7. PreparedStatement stmt = c.prepareStatement("INSERT INTO users(user_email, user_password, "
  8. + "user_bought, user_uuid, user_uuid_expire_date) VALUES (?, ?, "
  9. + "FALSE, '0', ?);");
  10. stmt.setString(1, user.getEmail());
  11. stmt.setString(2, user.getPassword());
  12. Calendar calen = Calendar.getInstance();
  13. calen.setTime(new java.util.Date());
  14. calen.set(Calendar.DAY_OF_MONTH, calen.get(Calendar.DAY_OF_MONTH) - 1);
  15. java.util.Date expireDate = calen.getTime();
  16. stmt.setDate(3, new Date(expireDate.getTime()));
  17. stmt.execute();
  18. stmt = c.prepareStatement("INSERT INTO users_to_roles(user_email, role_name) VALUES (?, 'user');");
  19. stmt.setString(1, user.getEmail());
  20. stmt.execute();
  21. c.commit();
  22. stmt.close();
  23. c.close();
  24. }
  25.  
  26. public boolean ifEmailRegistered(String email) throws SQLException {
  27. Connection c = DBConnectionManager.INSTANCE.getConnection();
  28. l.trace("Connection established");
  29. PreparedStatement stmt = c.prepareStatement("SELECT * FROM users WHERE user_email=?;");
  30. stmt.setString(1, email);
  31. ResultSet data = stmt.executeQuery();
  32. while (data.next()) {
  33. return true;
  34. }
  35. data.close();
  36. stmt.close();
  37. c.close();
  38. return false;
  39. }
  40.  
  41. public String findEmailByUUID(String uuid) throws SQLException {
  42. Connection c = DBConnectionManager.INSTANCE.getConnection();
  43. l.trace("Connection established");
  44. String username = null;
  45. PreparedStatement stmt = c.prepareStatement("SELECT user_email FROM users WHERE user_uuid=?;");
  46. stmt.setString(1, uuid);
  47. ResultSet rs = stmt.executeQuery();
  48. while (rs.next()) {
  49. username = rs.getString("user_email");
  50. }
  51. rs.close();
  52. stmt.close();
  53. c.close();
  54. return username;
  55. }
  56.  
  57. public User findUserByEmail(String email) throws SQLException {
  58. User r = new User();
  59. Connection c = DBConnectionManager.INSTANCE.getConnection();
  60. l.trace("Connection established");
  61. PreparedStatement stmt = c.prepareStatement("SELECT user_password, user_bought FROM users WHERE user_email=?;");
  62. stmt.setString(1, email);
  63. ResultSet rs = stmt.executeQuery();
  64. while (rs.next()) {
  65. r.setEmail(email);
  66. r.setBought(rs.getBoolean(2));
  67. r.setPasswordRaw(rs.getString(1));
  68. }
  69. rs.close();
  70. stmt.close();
  71. c.close();
  72. return r;
  73. }
  74.  
  75. public void setUUID(String email, String uuid) throws SQLException {
  76. Connection connection = DBConnectionManager.INSTANCE.getConnection();
  77. l.trace("Connection established");
  78. PreparedStatement stmt = null;
  79. Calendar c = Calendar.getInstance();
  80. c.setTime(new java.util.Date());
  81. if (uuid.equals(User.EMPTY_UUID)) {
  82. c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) - 1);
  83. } else {
  84. c.add(Calendar.DATE, 30);
  85. }
  86. java.util.Date expireDate = c.getTime();
  87. stmt = connection.prepareStatement("UPDATE users SET user_uuid=?, user_uuid_expire_date=? " + "WHERE user_email = ?;");
  88. stmt.setString(1, uuid);
  89. stmt.setDate(2, new Date(expireDate.getTime()));
  90. stmt.setString(3, email);
  91. stmt.execute();
  92. l.trace("Added UUID to user " + email);
  93. stmt.close();
  94. connection.close();
  95. }
  96.  
  97. public void deleteUUID(String email) throws SQLException {
  98. setUUID(email, User.EMPTY_UUID);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement