@Slf4j
public class UsersRepository {
private final JdbcDatabaseConnectionManager connectionManager;
public UsersRepository(JdbcDatabaseConnectionManager connectionManager) {
this.connectionManager = connectionManager;
}
public Users save(Users user) {
String sql = "INSERT INTO users (userId, userName, age) VALUES (?, ?, ?)";
Connection con = null;
PreparedStatement pstmt = null;
try {
// 데이터베이스 연결
con = connectionManager.getConnection();
// PreparedStatement 생성
pstmt = con.prepareStatement(sql);
// PreparedStatement 파라미터 설정
pstmt.setString(1, user.getUserId());
pstmt.setString(2, user.getUserName());
pstmt.setInt(3, user.getAge());
// 쿼리 실행
int affectedRows = pstmt.executeUpdate();
log.info("affectedRows = {}", affectedRows);
return user;
} catch (SQLException e) {
log.error("Failed to save user: {}", e.getMessage());
throw new RuntimeException("Failed to save user", e);
} finally {
// 자원 해제
try {
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
connectionManager.closeConnection(con);
}
} catch (SQLException e) {
log.error("Failed to close resources", e);
}
}
}
// 사용자 조회 로직 (findById)
public Users findById(String userId) {
String sql = "SELECT * FROM users WHERE userId = ?";
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 데이터베이스 연결
con = connectionManager.getConnection();
// PreparedStatement 생성
pstmt = con.prepareStatement(sql);
pstmt.setString(1, userId);
// 쿼리 실행 및 결과 처리
rs = pstmt.executeQuery();
if (rs.next()) {
Users user = new Users();
user.setUserId(rs.getString("userId"));
user.setUserName(rs.getString("userName"));
user.setAge(rs.getInt("age"));
log.info("Found user: {}", user);
return user;
} else {
log.warn("User with ID {} not found", userId);
return null;
}
} catch (SQLException e) {
log.error("Failed to find user: {}", e.getMessage());
throw new RuntimeException("Failed to find user", e);
} finally {
// 자원 해제
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
connectionManager.closeConnection(con);
}
} catch (SQLException e) {
log.error("Failed to close resources", e);
}
}
}
// 사용자 업데이트 로직 (update)
public Users update(Users user) {
String sql = "UPDATE users SET userName = ?, age = ? WHERE userId = ?";
Connection con = null;
PreparedStatement pstmt = null;
try {
// 데이터베이스 연결
con = connectionManager.getConnection();
// PreparedStatement 생성
pstmt = con.prepareStatement(sql);
// PreparedStatement 파라미터 설정
pstmt.setString(1, user.getUserName());
pstmt.setInt(2, user.getAge());
pstmt.setString(3, user.getUserId());
// 쿼리 실행
int affectedRows = pstmt.executeUpdate();
log.info("Updated affectedRows = {}", affectedRows);
if (affectedRows > 0) {
log.info("User updated successfully: {}", user.getUserId());
return user;
} else {
log.warn("No user found with ID {}", user.getUserId());
return null;
}
} catch (SQLException e) {
log.error("Failed to update user: {}", e.getMessage());
throw new RuntimeException("Failed to update user", e);
} finally {
// 자원 해제
try {
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
connectionManager.closeConnection(con);
}
} catch (SQLException e) {
log.error("Failed to close resources", e);
}
}
}
// 사용자 삭제 로직 (deleteById)
public void deleteById(String userId) {
String sql = "DELETE FROM users WHERE userId = ?";
Connection con = null;
PreparedStatement pstmt = null;
try {
// 데이터베이스 연결
con = connectionManager.getConnection();
// PreparedStatement 생성
pstmt = con.prepareStatement(sql);
pstmt.setString(1, userId);
// 쿼리 실행
int affectedRows = pstmt.executeUpdate();
log.info("Deleted affectedRows = {}", affectedRows);
if (affectedRows > 0) {
log.info("User with ID {} deleted successfully", userId);
} else {
log.warn("No user found with ID {}", userId);
}
} catch (SQLException e) {
log.error("Failed to delete user: {}", e.getMessage());
throw new RuntimeException("Failed to delete user", e);
} finally {
// 자원 해제
try {
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
connectionManager.closeConnection(con);
}
} catch (SQLException e) {
log.error("Failed to close resources", e);
}
}
}
}