@Override public User update(User user) throws DAOException { String KOMA = ", "; StringBuilder UPDATE_USER = new StringBuilder("UPDATE users SET firstname = ") .append("'" + user.getFirstName() + "'") .append(KOMA) .append("lastname = ") .append("'" + user.getLastName() + "'") .append(KOMA) .append("email = ") .append("'" + user.getEmail() + "'") .append(KOMA) .append("password = "******"'" + user.getPassword() + "'") .append(" WHERE id = ") .append(String.valueOf(user.getId())) .append(";"); log.entry(user); Connection connection = null; try { connection = ConnectionHolder.getConnection(); Statement statement = connection.createStatement(); statement.executeUpdate(UPDATE_USER.toString()); } catch (SQLException ex) { log.error("SQLException during answer update query", ex); throw new DAOException(ex); } log.exit(user); return user; }
@Override public User create(User user) throws DAOException { log.entry(user); Connection connection = null; try { connection = ConnectionHolder.getConnection(); PreparedStatement statement = connection.prepareStatement(INSERT_USER, Statement.RETURN_GENERATED_KEYS); statement.setString(1, user.getFirstName()); statement.setString(2, user.getLastName()); statement.setString(3, user.getEmail()); statement.setString(4, user.getPassword()); statement.executeUpdate(); ResultSet generatedKeys = statement.getGeneratedKeys(); if (generatedKeys.next()) { user.setId(generatedKeys.getInt(1)); } } catch (SQLException ex) { log.error("SQLException during answer insert query", ex); throw new DAOException(ex); } log.exit(user); return user; }