@Override public User get(int id) { log.info("PostgreUserDAO # get user by ID"); log.entry(id); Connection connection = ConnectionHolder.getConnection(); User user = null; ResultSet rs = null; try { PreparedStatement pstm = connection.prepareStatement(GET_USER_BY_ID); pstm.setInt(1, id); pstm.execute(); rs = pstm.getResultSet(); if (rs.next()) { user = extractUser(rs); } } catch (SQLException ex) { log.error("SQLException during user getById query", ex); } finally { close(rs); close(connection); } log.exit(user); log.info("finished PostgreUserDAO # get user by ID"); return user; }
@Override public User create(User user) throws DAOException { log.info("PostgreUserDAO#create user"); log.entry(user); Connection connection = ConnectionHolder.getConnection(); try { PreparedStatement statement = connection.prepareStatement(INSERT_USER, Statement.RETURN_GENERATED_KEYS); int columnsCounter = 0; statement.setString(++columnsCounter, user.getEmail()); statement.setString(++columnsCounter, user.getPassword()); statement.setString(++columnsCounter, user.getName()); statement.setString(++columnsCounter, user.getSurname()); statement.setString(++columnsCounter, user.getImageUrl()); statement.executeUpdate(); ResultSet generatedKeys = statement.getGeneratedKeys(); if (generatedKeys.next()) { user.setId(generatedKeys.getInt(1)); } } catch (SQLException ex) { log.error("SQLException during user insert query", ex); throw new DAOException(ex); } finally { close(connection); } log.exit(user); log.info("finished PostgreUserDAO#create user"); return user; }
@Override public User getByEmail(String email) { log.info("PostgreUserDAO # get user by E-mail"); log.entry(email); Connection connection = ConnectionHolder.getConnection(); User user = null; ResultSet rs = null; try { PreparedStatement pstm = connection.prepareStatement(GET_USER_BY_EMAIL); int k = 1; pstm.setString(k++, email); pstm.execute(); rs = pstm.getResultSet(); if (rs.next()) { user = extractUser(rs); } } catch (SQLException ex) { log.error("SQLException during user getByEmail query", ex); } finally { close(rs); close(connection); } log.exit(user); log.info("finished PostgreUserDAO # get user by E-mail"); return user; }
@Override public void remove(int id) { log.info("PostgreUserDAO # delete user by ID"); log.entry(id); Connection connection = ConnectionHolder.getConnection(); try { PreparedStatement pstm = connection.prepareStatement(DELETE_USER); pstm.setInt(1, id); pstm.execute(); } catch (SQLException ex) { log.error("SQLException during user delete query", ex); } finally { close(connection); } log.info("finished removing"); }
@Override public void update(User user) { log.info("PostgreUserDAO # update user"); log.entry(user); Connection connection = ConnectionHolder.getConnection(); try { PreparedStatement pstm = connection.prepareStatement(UPDATE_USER); int columnsCounter = 0; pstm.setString(++columnsCounter, user.getEmail()); pstm.setString(++columnsCounter, user.getPassword()); pstm.setString(++columnsCounter, user.getName()); pstm.setString(++columnsCounter, user.getSurname()); pstm.setString(++columnsCounter, user.getImageUrl()); pstm.execute(); } catch (SQLException ex) { log.error("SQLException during user update query", ex); } finally { close(connection); } log.info("finished updating"); }
@Override public List<User> getAll() throws DAOException { log.info("PostgreUserDAO # get all users from db"); Connection connection = ConnectionHolder.getConnection(); List<User> usersList = new ArrayList<User>(); Statement stm = null; ResultSet rs = null; try { stm = connection.createStatement(); stm.executeQuery(SELECT_ALL); rs = stm.getResultSet(); while (rs.next()) { usersList.add(extractUser(rs)); } } catch (SQLException ex) { log.error("SQLException during user getAll query", ex); } finally { close(rs); close(stm); } log.info("Finished getAll method"); return usersList; }