@Override public User retrieveUser(User user) { final String SQL = "SELECT * FROM users WHERE username = ?"; PreparedStatement preparedStatement = PreparedStatementCreator.createPreparedStatement(SQL); ResultSet resultSet = null; try { preparedStatement.setString(1, user.getUsername()); resultSet = preparedStatement.executeQuery(); rowAffected = resultSet.getFetchSize(); System.out.println("Row: " + rowAffected); } catch (SQLException e) { e.printStackTrace(); } User retrievedUser = null; try { while (resultSet.next()) { retrievedUser = new User(); retrievedUser.setFirstName(resultSet.getString("first_name")); retrievedUser.setLastName(resultSet.getString("last_name")); retrievedUser.setUsername(resultSet.getString("username")); retrievedUser.setEmail(resultSet.getString("email")); retrievedUser.setPassword(resultSet.getString("password")); retrievedUser.setType(resultSet.getString("user_type")); } } catch (SQLException e) { e.printStackTrace(); } return retrievedUser; }
@Override public Integer updateUser(User oldUserInfo, User newUserInfo) { final String SQL = "UPDATE users SET email = ? WHERE email = ?"; PreparedStatement preparedStatement = PreparedStatementCreator.createPreparedStatement(SQL); try { preparedStatement.setString(1, newUserInfo.getEmail()); preparedStatement.setString(2, oldUserInfo.getEmail()); } catch (SQLException e) { e.printStackTrace(); } Integer numberOfUpdates = null; try { numberOfUpdates = preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return numberOfUpdates; }
@Override public Integer createUser(User user) { final String query = "INSERT INTO users (first_name, last_name, username, email, password, contact, address, user_type) values (?, ?, ?, ?, ?, ?, ?, ?)"; PreparedStatement preparedStatement = PreparedStatementCreator.createPreparedStatement(query); Integer numberOfRecordsAdded = 0; try { preparedStatement.setString(1, user.getFirstName()); preparedStatement.setString(2, user.getLastName()); preparedStatement.setString(3, user.getUsername()); preparedStatement.setString(4, user.getEmail()); preparedStatement.setString(5, user.getPassword()); preparedStatement.setString(6, user.getContactNumber()); preparedStatement.setString(7, user.getAddress()); preparedStatement.setString(8, user.getType()); numberOfRecordsAdded = preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } return numberOfRecordsAdded; }