Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final Logger l = Logger.getLogger(UserDAO.class);
- public void register(User user) throws SQLException {
- Connection c = DBConnectionManager.INSTANCE.getConnection();
- l.trace("Connection established");
- c.setAutoCommit(false);
- PreparedStatement stmt = c.prepareStatement("INSERT INTO users(user_email, user_password, "
- + "user_bought, user_uuid, user_uuid_expire_date) VALUES (?, ?, "
- + "FALSE, '0', ?);");
- stmt.setString(1, user.getEmail());
- stmt.setString(2, user.getPassword());
- Calendar calen = Calendar.getInstance();
- calen.setTime(new java.util.Date());
- calen.set(Calendar.DAY_OF_MONTH, calen.get(Calendar.DAY_OF_MONTH) - 1);
- java.util.Date expireDate = calen.getTime();
- stmt.setDate(3, new Date(expireDate.getTime()));
- stmt.execute();
- stmt = c.prepareStatement("INSERT INTO users_to_roles(user_email, role_name) VALUES (?, 'user');");
- stmt.setString(1, user.getEmail());
- stmt.execute();
- c.commit();
- stmt.close();
- c.close();
- }
- public boolean ifEmailRegistered(String email) throws SQLException {
- Connection c = DBConnectionManager.INSTANCE.getConnection();
- l.trace("Connection established");
- PreparedStatement stmt = c.prepareStatement("SELECT * FROM users WHERE user_email=?;");
- stmt.setString(1, email);
- ResultSet data = stmt.executeQuery();
- while (data.next()) {
- return true;
- }
- data.close();
- stmt.close();
- c.close();
- return false;
- }
- public String findEmailByUUID(String uuid) throws SQLException {
- Connection c = DBConnectionManager.INSTANCE.getConnection();
- l.trace("Connection established");
- String username = null;
- PreparedStatement stmt = c.prepareStatement("SELECT user_email FROM users WHERE user_uuid=?;");
- stmt.setString(1, uuid);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- username = rs.getString("user_email");
- }
- rs.close();
- stmt.close();
- c.close();
- return username;
- }
- public User findUserByEmail(String email) throws SQLException {
- User r = new User();
- Connection c = DBConnectionManager.INSTANCE.getConnection();
- l.trace("Connection established");
- PreparedStatement stmt = c.prepareStatement("SELECT user_password, user_bought FROM users WHERE user_email=?;");
- stmt.setString(1, email);
- ResultSet rs = stmt.executeQuery();
- while (rs.next()) {
- r.setEmail(email);
- r.setBought(rs.getBoolean(2));
- r.setPasswordRaw(rs.getString(1));
- }
- rs.close();
- stmt.close();
- c.close();
- return r;
- }
- public void setUUID(String email, String uuid) throws SQLException {
- Connection connection = DBConnectionManager.INSTANCE.getConnection();
- l.trace("Connection established");
- PreparedStatement stmt = null;
- Calendar c = Calendar.getInstance();
- c.setTime(new java.util.Date());
- if (uuid.equals(User.EMPTY_UUID)) {
- c.set(Calendar.DAY_OF_MONTH, c.get(Calendar.DAY_OF_MONTH) - 1);
- } else {
- c.add(Calendar.DATE, 30);
- }
- java.util.Date expireDate = c.getTime();
- stmt = connection.prepareStatement("UPDATE users SET user_uuid=?, user_uuid_expire_date=? " + "WHERE user_email = ?;");
- stmt.setString(1, uuid);
- stmt.setDate(2, new Date(expireDate.getTime()));
- stmt.setString(3, email);
- stmt.execute();
- l.trace("Added UUID to user " + email);
- stmt.close();
- connection.close();
- }
- public void deleteUUID(String email) throws SQLException {
- setUUID(email, User.EMPTY_UUID);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement