Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1단계 - JDBC 라이브러리 구현하기] 베베(최원용) 미션 제출합니다. #264

Merged
merged 10 commits into from
Sep 28, 2023
198 changes: 25 additions & 173 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,207 +4,59 @@
import org.springframework.jdbc.core.JdbcTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.RowMapper;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class UserDao {

private static final Logger log = LoggerFactory.getLogger(UserDao.class);

private final DataSource dataSource;

public UserDao(final DataSource dataSource) {
this.dataSource = dataSource;
}
private final JdbcTemplate jdbcTemplate;

public UserDao(final JdbcTemplate jdbcTemplate) {
this.dataSource = null;
this.jdbcTemplate = jdbcTemplate;
}

public void insert(final User user) {
final String sql = "insert into users (account, password, email) values (?, ?, ?)";

Connection conn = null;
PreparedStatement preparedStatement = null;
try {
conn = dataSource.getConnection();
preparedStatement = conn.prepareStatement(sql);

log.debug("query : {}", sql);

preparedStatement.setString(1, user.getAccount());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getEmail());
preparedStatement.executeUpdate();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException ignored) {}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {}
}
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail());
}

public void update(final User user) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
final String sql = "update users set account=?, password=?, email=? where id=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, user.getAccount());
preparedStatement.setString(2, user.getPassword());
preparedStatement.setString(3, user.getEmail());
preparedStatement.setLong(4, user.getId());
preparedStatement.executeUpdate();
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException ignored) {}

try {
if (connection != null) {
connection.close();
}
} catch (SQLException ignored) {}
}
final String sql = "update users set account=?, password=?, email=? where id=?";
jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId());
}

public List<User> findAll() {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
final String sql = "select * from users";
preparedStatement = connection.prepareStatement(sql);
final ResultSet resultSet = preparedStatement.executeQuery();

final List<User> users = new ArrayList<>();
while (resultSet.next()) {
final Long id = resultSet.getLong("id");
final String account = resultSet.getString("account");
final String password = resultSet.getString("password");
final String email = resultSet.getString("email");
users.add(new User(id, account, password, email));
}
return users;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException ignored) {}

try {
if (connection != null) {
connection.close();
}
} catch (SQLException ignored) {}
}
final String sql = "select * from users";
return jdbcTemplate.query(sql, getUserRowMapper());
}

public User findById(final Long id) {
final var sql = "select id, account, password, email from users where id = ?";

Connection conn = null;
PreparedStatement preparedStatement = null;
ResultSet rs = null;
try {
conn = dataSource.getConnection();
preparedStatement = conn.prepareStatement(sql);
preparedStatement.setLong(1, id);
rs = preparedStatement.executeQuery();

log.debug("query : {}", sql);

if (rs.next()) {
return new User(
rs.getLong(1),
rs.getString(2),
rs.getString(3),
rs.getString(4));
}
return null;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (rs != null) {
rs.close();
}
} catch (SQLException ignored) {}

try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException ignored) {}

try {
if (conn != null) {
conn.close();
}
} catch (SQLException ignored) {}
}
private RowMapper<User> getUserRowMapper() {
return ((resultSet, rowNum) ->
new User(
resultSet.getLong("id"),
resultSet.getString("account"),
resultSet.getString("password"),
resultSet.getString("email")
)
);
wonyongChoi05 marked this conversation as resolved.
Show resolved Hide resolved
}

public User findByAccount(final String account) {
Connection connection = null;
PreparedStatement preparedStatement = null;
try {
connection = dataSource.getConnection();
final String sql = "select * from users where account = ?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, account);
final ResultSet resultSet = preparedStatement.executeQuery();

while (resultSet.next()) {
final Long id = resultSet.getLong("id");
final String password = resultSet.getString("password");
final String email = resultSet.getString("email");
return new User(id, account, password, email);
}
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (preparedStatement != null) {
preparedStatement.close();
}
} catch (SQLException ignored) {}
public Optional<User> findById(final Long id) {
final String sql = "select id, account, password, email from users where id = ?";
return jdbcTemplate.queryForObject(sql, getUserRowMapper(), id);
}

try {
if (connection != null) {
connection.close();
}
} catch (SQLException ignored) {}
}
return null;
public Optional<User> findByAccount(final String account) {
final String sql = "select * from users where account = ?";
return jdbcTemplate.queryForObject(sql, getUserRowMapper(), account);
}

}
82 changes: 82 additions & 0 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,17 @@

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

public class JdbcTemplate {

Expand All @@ -14,4 +23,77 @@ public class JdbcTemplate {
public JdbcTemplate(final DataSource dataSource) {
this.dataSource = dataSource;
}

public void update(final String sql, final Object... parameters) {
try (
final Connection connection = getConnection();
final PreparedStatement preparedStatement = connection.prepareStatement(sql)
) {
log.debug("query : {}", sql);
for (int i = 1; i <= parameters.length; i++) {
preparedStatement.setObject(i, parameters[i - 1]);
}
preparedStatement.executeUpdate();
} catch (SQLException e) {
throw new DataAccessException(e);
}
}

public <T> Optional<T> queryForObject(final String sql, final RowMapper<T> rowMapper, final Object condition) {
try (
final Connection connection = getConnection();
final PreparedStatement preparedStatement = connection.prepareStatement(sql)
) {
preparedStatement.setObject(1, condition);
final ResultSet resultSet = preparedStatement.executeQuery();
log.debug("query : {}", sql);

if (resultSet.next()) {
final T result = rowMapper.mapRow(resultSet, resultSet.getRow());
resultSet.close();
return Optional.ofNullable(result);
}
resultSet.close();
return Optional.empty();
} catch (SQLException e) {
throw new DataAccessException(e);
}
}

public <T> List<T> query(final String sql, final RowMapper<T> rowMapper) {
try (
final Connection connection = getConnection();
final PreparedStatement preparedStatement = connection.prepareStatement(sql);
final ResultSet resultSet = preparedStatement.executeQuery()
) {
log.debug("query : {}", sql);

final List<T> results = new ArrayList<>();
while (resultSet.next()) {
results.add(rowMapper.mapRow(resultSet, resultSet.getRow()));
}
return results;
} catch (SQLException e) {
throw new DataAccessException(e);
}
}

public Connection getConnection() {
Connection connection = null;
try {
connection = dataSource.getConnection();
return connection;
} catch (SQLException e) {
log.error(e.getMessage(), e);
throw new RuntimeException(e);
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException ignored) {
}
}
}

}
11 changes: 11 additions & 0 deletions jdbc/src/main/java/org/springframework/jdbc/core/RowMapper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.springframework.jdbc.core;

import java.sql.ResultSet;
import java.sql.SQLException;

@FunctionalInterface
public interface RowMapper<T> {

T mapRow(final ResultSet resultSet, final int rowNumber) throws SQLException;

}
wonyongChoi05 marked this conversation as resolved.
Show resolved Hide resolved