package com.sample.app.usermanagement.dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import com.sample.app.usermanagement.model.User; /** * DAO class handling the CRUD database operations for the user application. * * @author Bill Chan AWS * * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * SPDX-License-Identifier: MIT-0 * */ public class UserDAO { // Externalize and update jdbcURL, jdbcUsername, jdbcPassword parameters specific to your environment private String jdbcURL = "jdbc:mysql://:3306/webappdb?useSSL=false"; private String jdbcUsername = ""; private String jdbcPassword = ""; private static final String INSERT_USERS_SQL = "INSERT INTO users" + " (name, email, company) VALUES " + " (?, ?, ?);"; private static final String SELECT_USER_BY_ID = "select id,name,email,company from users where id =?"; private static final String SELECT_ALL_USERS = "select * from users"; private static final String DELETE_USERS_SQL = "delete from users where id = ?;"; private static final String UPDATE_USERS_SQL = "update users set name = ?,email= ?, company =? where id = ?;"; public UserDAO() { } protected Connection getConnection() { Connection connection = null; try { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection(jdbcURL, jdbcUsername, jdbcPassword); } catch (SQLException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } return connection; } public void insertUser(User user) throws SQLException { System.out.println(INSERT_USERS_SQL); try (Connection connection = getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USERS_SQL)) { preparedStatement.setString(1, user.getName()); preparedStatement.setString(2, user.getEmail()); preparedStatement.setString(3, user.getCompany()); System.out.println(preparedStatement); preparedStatement.executeUpdate(); } catch (SQLException e) { printSQLException(e); } } public User selectUser(int id) { User user = null; try (Connection connection = getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(SELECT_USER_BY_ID);) { preparedStatement.setInt(1, id); System.out.println(preparedStatement); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { String name = rs.getString("name"); String email = rs.getString("email"); String company = rs.getString("company"); user = new User(id, name, email, company); } } catch (SQLException e) { printSQLException(e); } return user; } public List selectAllUsers() { List users = new ArrayList<>(); try (Connection connection = getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_USERS);) { System.out.println(preparedStatement); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String name = rs.getString("name"); String email = rs.getString("email"); String company = rs.getString("company"); users.add(new User(id, name, email, company)); } } catch (SQLException e) { printSQLException(e); } return users; } public boolean deleteUser(int id) throws SQLException { boolean rowDeleted; try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(DELETE_USERS_SQL);) { statement.setInt(1, id); rowDeleted = statement.executeUpdate() > 0; } return rowDeleted; } public boolean updateUser(User user) throws SQLException { boolean rowUpdated; try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(UPDATE_USERS_SQL);) { statement.setString(1, user.getName()); statement.setString(2, user.getEmail()); statement.setString(3, user.getCompany()); statement.setInt(4, user.getId()); rowUpdated = statement.executeUpdate() > 0; } return rowUpdated; } private void printSQLException(SQLException ex) { for (Throwable e : ex) { if (e instanceof SQLException) { e.printStackTrace(System.err); System.err.println("SQLState: " + ((SQLException) e).getSQLState()); System.err.println("Error Code: " + ((SQLException) e).getErrorCode()); System.err.println("Message: " + e.getMessage()); Throwable t = ex.getCause(); while (t != null) { System.out.println("Cause: " + t); t = t.getCause(); } } } } }