From d5725919b837dd461ce08464d17edb43c57cdad6 Mon Sep 17 00:00:00 2001 From: devHudi Date: Tue, 4 Oct 2022 00:35:12 +0900 Subject: [PATCH 01/13] =?UTF-8?q?feat:=20update=20=EB=B0=8F=20query=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 122 +++++++----------- .../java/com/techcourse/dao/UserDaoTest.java | 4 +- .../main/java/nextstep/jdbc/JdbcTemplate.java | 46 ++++++- .../main/java/nextstep/jdbc/RowMapper.java | 9 ++ 4 files changed, 99 insertions(+), 82 deletions(-) create mode 100644 jdbc/src/main/java/nextstep/jdbc/RowMapper.java diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index ee17c159c2..5306b68dea 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,121 +1,87 @@ package com.techcourse.dao; import com.techcourse.domain.User; +import java.sql.SQLException; +import java.util.List; +import javax.sql.DataSource; +import nextstep.jdbc.DataAccessException; import nextstep.jdbc.JdbcTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.sql.DataSource; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.List; - public class UserDao { private static final Logger log = LoggerFactory.getLogger(UserDao.class); private final DataSource dataSource; + private final JdbcTemplate jdbcTemplate; public UserDao(final DataSource dataSource) { this.dataSource = dataSource; + this.jdbcTemplate = new JdbcTemplate(dataSource); } public UserDao(final JdbcTemplate jdbcTemplate) { this.dataSource = null; + this.jdbcTemplate = jdbcTemplate; } public void insert(final User user) { - final var sql = "insert into users (account, password, email) values (?, ?, ?)"; - - Connection conn = null; - PreparedStatement pstmt = null; - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - - log.debug("query : {}", sql); - - pstmt.setString(1, user.getAccount()); - pstmt.setString(2, user.getPassword()); - pstmt.setString(3, user.getEmail()); - pstmt.executeUpdate(); - } catch (SQLException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); - } finally { - try { - if (pstmt != null) { - pstmt.close(); - } - } catch (SQLException ignored) {} - - try { - if (conn != null) { - conn.close(); - } - } catch (SQLException ignored) {} - } + String sql = "insert into users (account, password, email) values (?, ?, ?)"; + jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail()); } public void update(final User user) { - // todo + String sql = "update users set (account, password, email) = (?, ?, ?) where id = ?"; + jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId()); } public List findAll() { - // todo - return null; + String sql = "select id, account, password, email from users"; + return jdbcTemplate.query(sql, rs -> { + try { + return new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4)); + } catch (SQLException e) { + throw new DataAccessException(); + } + }); } public User findById(final Long id) { - final var sql = "select id, account, password, email from users where id = ?"; - - Connection conn = null; - PreparedStatement pstmt = null; - ResultSet rs = null; - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - pstmt.setLong(1, id); - rs = pstmt.executeQuery(); - - log.debug("query : {}", sql); - - if (rs.next()) { + String sql = "select id, account, password, email from users where id = ?"; + List users = jdbcTemplate.query(sql, rs -> { + try { return new User( rs.getLong(1), rs.getString(2), rs.getString(3), rs.getString(4)); + } catch (SQLException e) { + throw new DataAccessException(); } - 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 (pstmt != null) { - pstmt.close(); - } - } catch (SQLException ignored) {} + }, id); - try { - if (conn != null) { - conn.close(); - } - } catch (SQLException ignored) {} - } + return users.get(0); } public User findByAccount(final String account) { - // todo - return null; + String sql = "select id, account, password, email from users where account = ?"; + List users = jdbcTemplate.query(sql, rs -> { + try { + return new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4)); + } catch (SQLException e) { + throw new DataAccessException(); + } + }, account); + + return users.get(0); } } diff --git a/app/src/test/java/com/techcourse/dao/UserDaoTest.java b/app/src/test/java/com/techcourse/dao/UserDaoTest.java index 773d7faf82..2bcf5a8a40 100644 --- a/app/src/test/java/com/techcourse/dao/UserDaoTest.java +++ b/app/src/test/java/com/techcourse/dao/UserDaoTest.java @@ -1,13 +1,13 @@ package com.techcourse.dao; +import static org.assertj.core.api.Assertions.assertThat; + import com.techcourse.config.DataSourceConfig; import com.techcourse.domain.User; import com.techcourse.support.jdbc.init.DatabasePopulatorUtils; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import static org.assertj.core.api.Assertions.assertThat; - class UserDaoTest { private UserDao userDao; diff --git a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java index 613a588a93..9811895c79 100644 --- a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java +++ b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java @@ -1,10 +1,15 @@ package nextstep.jdbc; +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 javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import javax.sql.DataSource; - public class JdbcTemplate { private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); @@ -14,4 +19,41 @@ public class JdbcTemplate { public JdbcTemplate(final DataSource dataSource) { this.dataSource = dataSource; } + + public int update(final String sql, final Object... args) { + try (Connection connection = dataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + + for (int i = 0; i < args.length; i++) { + preparedStatement.setObject(i + 1, args[i]); + } + + return preparedStatement.executeUpdate(); + } catch (final SQLException e) { + throw new DataAccessException(e.getMessage(), e); + // TODO: 더 추상화된 예외 메시지를 사용해야함 + } + } + + public List query(final String sql, final RowMapper rowMapper, final Object... args) { + try (Connection connection = dataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + + for (int i = 0; i < args.length; i++) { + preparedStatement.setObject(i + 1, args[i]); + } + + List results = new ArrayList<>(); + ResultSet rs = preparedStatement.executeQuery(); + if (rs.next()) { + T result = rowMapper.run(rs); + results.add(result); + } + + return results; + } catch (final SQLException e) { + throw new DataAccessException(e.getMessage(), e); + // TODO: 더 추상화된 예외 메시지를 사용해야함 + } + } } diff --git a/jdbc/src/main/java/nextstep/jdbc/RowMapper.java b/jdbc/src/main/java/nextstep/jdbc/RowMapper.java new file mode 100644 index 0000000000..7e6db66bc6 --- /dev/null +++ b/jdbc/src/main/java/nextstep/jdbc/RowMapper.java @@ -0,0 +1,9 @@ +package nextstep.jdbc; + +import java.sql.ResultSet; + +@FunctionalInterface +public interface RowMapper { + + T run(final ResultSet rs); +} From 4c0bb9f8c77cab7010c540d6248cef60a9e2ace1 Mon Sep 17 00:00:00 2001 From: devHudi Date: Tue, 4 Oct 2022 00:42:56 +0900 Subject: [PATCH 02/13] =?UTF-8?q?feat:=20queryForObject=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 9 +++----- .../main/java/nextstep/jdbc/JdbcTemplate.java | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 5306b68dea..84563be2f1 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -36,6 +36,7 @@ public void update(final User user) { jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId()); } + public List findAll() { String sql = "select id, account, password, email from users"; return jdbcTemplate.query(sql, rs -> { @@ -53,7 +54,7 @@ public List findAll() { public User findById(final Long id) { String sql = "select id, account, password, email from users where id = ?"; - List users = jdbcTemplate.query(sql, rs -> { + return jdbcTemplate.queryForObject(sql, rs -> { try { return new User( rs.getLong(1), @@ -64,13 +65,11 @@ public User findById(final Long id) { throw new DataAccessException(); } }, id); - - return users.get(0); } public User findByAccount(final String account) { String sql = "select id, account, password, email from users where account = ?"; - List users = jdbcTemplate.query(sql, rs -> { + return jdbcTemplate.queryForObject(sql, rs -> { try { return new User( rs.getLong(1), @@ -81,7 +80,5 @@ public User findByAccount(final String account) { throw new DataAccessException(); } }, account); - - return users.get(0); } } diff --git a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java index 9811895c79..b9639fdf3b 100644 --- a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java +++ b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java @@ -56,4 +56,25 @@ public List query(final String sql, final RowMapper rowMapper, final O // TODO: 더 추상화된 예외 메시지를 사용해야함 } } + + public T queryForObject(final String sql, final RowMapper rowMapper, final Object... args) { + try (Connection connection = dataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(sql)) { + + for (int i = 0; i < args.length; i++) { + preparedStatement.setObject(i + 1, args[i]); + } + + T result = null; + ResultSet rs = preparedStatement.executeQuery(); + if (rs.next()) { + result = rowMapper.run(rs); + } + + return result; + } catch (final SQLException e) { + throw new DataAccessException(e.getMessage(), e); + // TODO: 더 추상화된 예외 메시지를 사용해야함 + } + } } From 18f400151dda28bee7373920c0d35d0ae1c6d637 Mon Sep 17 00:00:00 2001 From: devHudi Date: Tue, 4 Oct 2022 01:21:47 +0900 Subject: [PATCH 03/13] =?UTF-8?q?refactor:=20UserDao=EC=9D=98=20DataSource?= =?UTF-8?q?=20=ED=95=84=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/techcourse/dao/UserDao.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 84563be2f1..62596a78ff 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -13,19 +13,16 @@ public class UserDao { private static final Logger log = LoggerFactory.getLogger(UserDao.class); - private final DataSource dataSource; private final JdbcTemplate jdbcTemplate; - public UserDao(final DataSource dataSource) { - this.dataSource = dataSource; - this.jdbcTemplate = new JdbcTemplate(dataSource); - } - public UserDao(final JdbcTemplate jdbcTemplate) { - this.dataSource = null; this.jdbcTemplate = jdbcTemplate; } + public UserDao(final DataSource dataSource) { + this.jdbcTemplate = new JdbcTemplate(dataSource); + } + public void insert(final User user) { String sql = "insert into users (account, password, email) values (?, ?, ?)"; jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail()); @@ -36,7 +33,6 @@ public void update(final User user) { jdbcTemplate.update(sql, user.getAccount(), user.getPassword(), user.getEmail(), user.getId()); } - public List findAll() { String sql = "select id, account, password, email from users"; return jdbcTemplate.query(sql, rs -> { From e52077ce82936ca43f7d5f56a9612e50a61e0f26 Mon Sep 17 00:00:00 2001 From: devHudi Date: Tue, 4 Oct 2022 01:39:45 +0900 Subject: [PATCH 04/13] =?UTF-8?q?refactor:=20=EB=9E=8C=EB=8B=A4=EB=A5=BC?= =?UTF-8?q?=20=EC=82=AC=EC=9A=A9=ED=95=9C=20=ED=85=9C=ED=94=8C=EB=A6=BF=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=ED=8C=A8=ED=84=B4=EC=9D=84=20?= =?UTF-8?q?=EC=82=AC=EC=9A=A9=ED=95=B4=20=EC=A4=91=EB=B3=B5=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/nextstep/jdbc/JdbcTemplate.java | 75 ++++++++++--------- 1 file changed, 40 insertions(+), 35 deletions(-) diff --git a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java index b9639fdf3b..c95b280c85 100644 --- a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java +++ b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java @@ -6,6 +6,7 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; +import java.util.function.Function; import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -21,43 +22,53 @@ public JdbcTemplate(final DataSource dataSource) { } public int update(final String sql, final Object... args) { - try (Connection connection = dataSource.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - - for (int i = 0; i < args.length; i++) { - preparedStatement.setObject(i + 1, args[i]); + return usePreparedStatement(sql, pstmt -> { + try { + return pstmt.executeUpdate(sql); + } catch (SQLException e) { + throw new DataAccessException(e.getMessage(), e); + // TODO: 더 추상화된 예외 메시지를 사용해야함 } - - return preparedStatement.executeUpdate(); - } catch (final SQLException e) { - throw new DataAccessException(e.getMessage(), e); - // TODO: 더 추상화된 예외 메시지를 사용해야함 - } + }, args); } public List query(final String sql, final RowMapper rowMapper, final Object... args) { - try (Connection connection = dataSource.getConnection(); - PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - - for (int i = 0; i < args.length; i++) { - preparedStatement.setObject(i + 1, args[i]); + return usePreparedStatement(sql, pstmt -> { + try { + List results = new ArrayList<>(); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) { + T result = rowMapper.run(rs); + results.add(result); + } + + return results; + } catch (SQLException e) { + throw new DataAccessException(e.getMessage(), e); + // TODO: 더 추상화된 예외 메시지를 사용해야함 } + }, args); + } - List results = new ArrayList<>(); - ResultSet rs = preparedStatement.executeQuery(); - if (rs.next()) { - T result = rowMapper.run(rs); - results.add(result); + public T queryForObject(final String sql, final RowMapper rowMapper, final Object... args) { + return usePreparedStatement(sql, pstmt -> { + try { + T result = null; + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) { + result = rowMapper.run(rs); + } + + return result; + } catch (SQLException e) { + throw new DataAccessException(e.getMessage(), e); + // TODO: 더 추상화된 예외 메시지를 사용해야함 } - - return results; - } catch (final SQLException e) { - throw new DataAccessException(e.getMessage(), e); - // TODO: 더 추상화된 예외 메시지를 사용해야함 - } + }, args); } - public T queryForObject(final String sql, final RowMapper rowMapper, final Object... args) { + public T usePreparedStatement(final String sql, final Function function, + final Object... args) { try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { @@ -65,13 +76,7 @@ public T queryForObject(final String sql, final RowMapper rowMapper, fina preparedStatement.setObject(i + 1, args[i]); } - T result = null; - ResultSet rs = preparedStatement.executeQuery(); - if (rs.next()) { - result = rowMapper.run(rs); - } - - return result; + return function.apply(preparedStatement); } catch (final SQLException e) { throw new DataAccessException(e.getMessage(), e); // TODO: 더 추상화된 예외 메시지를 사용해야함 From 32cf14d47b2be174be968f59a9adbb0c96777044 Mon Sep 17 00:00:00 2001 From: devHudi Date: Tue, 4 Oct 2022 01:50:30 +0900 Subject: [PATCH 05/13] =?UTF-8?q?refactor:=20User=EC=97=90=20=EB=8C=80?= =?UTF-8?q?=ED=95=9C=20RowMapper=EB=A5=BC=20=EC=83=81=EC=88=98=EB=A1=9C=20?= =?UTF-8?q?=EB=93=B1=EB=A1=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 48 ++++++------------- 1 file changed, 15 insertions(+), 33 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 62596a78ff..eb23d0bd75 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -6,12 +6,24 @@ import javax.sql.DataSource; import nextstep.jdbc.DataAccessException; import nextstep.jdbc.JdbcTemplate; +import nextstep.jdbc.RowMapper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class UserDao { private static final Logger log = LoggerFactory.getLogger(UserDao.class); + private static final RowMapper USER_ROW_MAPPER = rs -> { + try { + return new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4)); + } catch (SQLException e) { + throw new DataAccessException(); + } + }; private final JdbcTemplate jdbcTemplate; @@ -35,46 +47,16 @@ public void update(final User user) { public List findAll() { String sql = "select id, account, password, email from users"; - return jdbcTemplate.query(sql, rs -> { - try { - return new User( - rs.getLong(1), - rs.getString(2), - rs.getString(3), - rs.getString(4)); - } catch (SQLException e) { - throw new DataAccessException(); - } - }); + return jdbcTemplate.query(sql, USER_ROW_MAPPER); } public User findById(final Long id) { String sql = "select id, account, password, email from users where id = ?"; - return jdbcTemplate.queryForObject(sql, rs -> { - try { - return new User( - rs.getLong(1), - rs.getString(2), - rs.getString(3), - rs.getString(4)); - } catch (SQLException e) { - throw new DataAccessException(); - } - }, id); + return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, id); } public User findByAccount(final String account) { String sql = "select id, account, password, email from users where account = ?"; - return jdbcTemplate.queryForObject(sql, rs -> { - try { - return new User( - rs.getLong(1), - rs.getString(2), - rs.getString(3), - rs.getString(4)); - } catch (SQLException e) { - throw new DataAccessException(); - } - }, account); + return jdbcTemplate.queryForObject(sql, USER_ROW_MAPPER, account); } } From 60342cc751b747ac8454fbe6d77c901b1458267e Mon Sep 17 00:00:00 2001 From: devHudi Date: Wed, 5 Oct 2022 12:05:32 +0900 Subject: [PATCH 06/13] =?UTF-8?q?fix:=20executeUpdate=EC=97=90=20SQL?= =?UTF-8?q?=EC=9D=84=20=EC=A0=84=EB=8B=AC=ED=95=98=EB=8A=94=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=EB=A5=BC=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java index c95b280c85..93655c2da1 100644 --- a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java +++ b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java @@ -24,7 +24,7 @@ public JdbcTemplate(final DataSource dataSource) { public int update(final String sql, final Object... args) { return usePreparedStatement(sql, pstmt -> { try { - return pstmt.executeUpdate(sql); + return pstmt.executeUpdate(); } catch (SQLException e) { throw new DataAccessException(e.getMessage(), e); // TODO: 더 추상화된 예외 메시지를 사용해야함 From f5876a1454d16bd11399404cdfc284d09157227c Mon Sep 17 00:00:00 2001 From: devHudi Date: Wed, 5 Oct 2022 12:06:10 +0900 Subject: [PATCH 07/13] =?UTF-8?q?refactor:=20RowMapper=EC=9D=98=20run=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=EA=B0=80=20SQLException=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=EB=A5=BC=20=EB=AF=B8=EB=A3=A8=EB=8F=84=EB=A1=9D=20?= =?UTF-8?q?=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/techcourse/dao/UserDao.java | 19 ++++++------------- .../main/java/nextstep/jdbc/RowMapper.java | 3 ++- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index eb23d0bd75..4343205b20 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,10 +1,8 @@ package com.techcourse.dao; import com.techcourse.domain.User; -import java.sql.SQLException; import java.util.List; import javax.sql.DataSource; -import nextstep.jdbc.DataAccessException; import nextstep.jdbc.JdbcTemplate; import nextstep.jdbc.RowMapper; import org.slf4j.Logger; @@ -13,17 +11,12 @@ public class UserDao { private static final Logger log = LoggerFactory.getLogger(UserDao.class); - private static final RowMapper USER_ROW_MAPPER = rs -> { - try { - return new User( - rs.getLong(1), - rs.getString(2), - rs.getString(3), - rs.getString(4)); - } catch (SQLException e) { - throw new DataAccessException(); - } - }; + private static final RowMapper USER_ROW_MAPPER = rs -> new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4) + ); private final JdbcTemplate jdbcTemplate; diff --git a/jdbc/src/main/java/nextstep/jdbc/RowMapper.java b/jdbc/src/main/java/nextstep/jdbc/RowMapper.java index 7e6db66bc6..d481a9ef86 100644 --- a/jdbc/src/main/java/nextstep/jdbc/RowMapper.java +++ b/jdbc/src/main/java/nextstep/jdbc/RowMapper.java @@ -1,9 +1,10 @@ package nextstep.jdbc; import java.sql.ResultSet; +import java.sql.SQLException; @FunctionalInterface public interface RowMapper { - T run(final ResultSet rs); + T run(final ResultSet rs) throws SQLException; } From 1277be0c32a4fcb34f4567da575d6794c482fbc9 Mon Sep 17 00:00:00 2001 From: devHudi Date: Wed, 5 Oct 2022 13:14:03 +0900 Subject: [PATCH 08/13] =?UTF-8?q?refactor:=20queryForObject=EA=B0=80=20que?= =?UTF-8?q?ry=20=EB=A9=94=EC=86=8C=EB=93=9C=EB=A5=BC=20=EC=9E=AC=EC=82=AC?= =?UTF-8?q?=EC=9A=A9=ED=95=98=EB=8F=84=EB=A1=9D=20=ED=95=98=EC=97=AC=20?= =?UTF-8?q?=EC=A4=91=EB=B3=B5=20=EC=BD=94=EB=93=9C=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/nextstep/jdbc/JdbcTemplate.java | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java index 93655c2da1..7089885aaa 100644 --- a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java +++ b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java @@ -14,6 +14,7 @@ public class JdbcTemplate { private static final Logger log = LoggerFactory.getLogger(JdbcTemplate.class); + private static final int FIRST_RESULT_INDEX = 0; private final DataSource dataSource; @@ -51,20 +52,8 @@ public List query(final String sql, final RowMapper rowMapper, final O } public T queryForObject(final String sql, final RowMapper rowMapper, final Object... args) { - return usePreparedStatement(sql, pstmt -> { - try { - T result = null; - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) { - result = rowMapper.run(rs); - } - - return result; - } catch (SQLException e) { - throw new DataAccessException(e.getMessage(), e); - // TODO: 더 추상화된 예외 메시지를 사용해야함 - } - }, args); + List results = query(sql, rowMapper, args); + return results.get(FIRST_RESULT_INDEX); } public T usePreparedStatement(final String sql, final Function function, From 3ea42e7020aba87c697d0b34b51259bbda67b7aa Mon Sep 17 00:00:00 2001 From: devHudi Date: Wed, 5 Oct 2022 13:22:58 +0900 Subject: [PATCH 09/13] =?UTF-8?q?refactor:=20ThrowingFunction=20=ED=95=A8?= =?UTF-8?q?=EC=88=98=ED=98=95=20=EC=9D=B8=ED=84=B0=ED=8E=98=EC=9D=B4?= =?UTF-8?q?=EC=8A=A4=EB=A5=BC=20=EC=A0=95=EC=9D=98=ED=95=98=EC=97=AC=20?= =?UTF-8?q?=EB=B6=88=ED=95=84=EC=9A=94=ED=95=9C=20try=20-=20catch=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/nextstep/jdbc/JdbcTemplate.java | 32 ++++++------------- .../java/nextstep/jdbc/ThrowingFunction.java | 7 ++++ 2 files changed, 17 insertions(+), 22 deletions(-) create mode 100644 jdbc/src/main/java/nextstep/jdbc/ThrowingFunction.java diff --git a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java index 7089885aaa..58a78c6553 100644 --- a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java +++ b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java @@ -6,7 +6,6 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.List; -import java.util.function.Function; import javax.sql.DataSource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -23,31 +22,19 @@ public JdbcTemplate(final DataSource dataSource) { } public int update(final String sql, final Object... args) { - return usePreparedStatement(sql, pstmt -> { - try { - return pstmt.executeUpdate(); - } catch (SQLException e) { - throw new DataAccessException(e.getMessage(), e); - // TODO: 더 추상화된 예외 메시지를 사용해야함 - } - }, args); + return usePreparedStatement(sql, PreparedStatement::executeUpdate, args); } public List query(final String sql, final RowMapper rowMapper, final Object... args) { return usePreparedStatement(sql, pstmt -> { - try { - List results = new ArrayList<>(); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) { - T result = rowMapper.run(rs); - results.add(result); - } - - return results; - } catch (SQLException e) { - throw new DataAccessException(e.getMessage(), e); - // TODO: 더 추상화된 예외 메시지를 사용해야함 + List results = new ArrayList<>(); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) { + T result = rowMapper.run(rs); + results.add(result); } + + return results; }, args); } @@ -56,7 +43,8 @@ public T queryForObject(final String sql, final RowMapper rowMapper, fina return results.get(FIRST_RESULT_INDEX); } - public T usePreparedStatement(final String sql, final Function function, + public T usePreparedStatement(final String sql, + final ThrowingFunction function, final Object... args) { try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { diff --git a/jdbc/src/main/java/nextstep/jdbc/ThrowingFunction.java b/jdbc/src/main/java/nextstep/jdbc/ThrowingFunction.java new file mode 100644 index 0000000000..eb4127ab1a --- /dev/null +++ b/jdbc/src/main/java/nextstep/jdbc/ThrowingFunction.java @@ -0,0 +1,7 @@ +package nextstep.jdbc; + +@FunctionalInterface +public interface ThrowingFunction { + + R apply(final T t) throws E; +} From 6fbe88362a94ff54b91286fb862b1909715301d9 Mon Sep 17 00:00:00 2001 From: devHudi Date: Wed, 5 Oct 2022 13:33:54 +0900 Subject: [PATCH 10/13] =?UTF-8?q?feat:=20=EC=98=88=EC=99=B8=20=EB=B0=9C?= =?UTF-8?q?=EC=83=9D=EC=8B=9C=20=EB=A1=9C=EA=B7=B8=ED=95=98=EB=8F=84?= =?UTF-8?q?=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java | 1 + 1 file changed, 1 insertion(+) diff --git a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java index 58a78c6553..9fc78d30bd 100644 --- a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java +++ b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java @@ -55,6 +55,7 @@ public T usePreparedStatement(final String sql, return function.apply(preparedStatement); } catch (final SQLException e) { + log.error("error: {}", e); throw new DataAccessException(e.getMessage(), e); // TODO: 더 추상화된 예외 메시지를 사용해야함 } From 536a3b08c0275d71af9199f68422e6fe80158eaf Mon Sep 17 00:00:00 2001 From: devHudi Date: Wed, 5 Oct 2022 13:34:38 +0900 Subject: [PATCH 11/13] =?UTF-8?q?refactor:=20ResultSet=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=EA=B0=92=EC=9D=84=20=EA=BA=BC=EB=82=B4=EC=98=AC=20=EB=95=8C?= =?UTF-8?q?=20=EC=9D=B8=EB=8D=B1=EC=8A=A4=20=EB=8C=80=EC=8B=A0=20=EC=BB=AC?= =?UTF-8?q?=EB=9F=BC=20=EC=9D=B4=EB=A6=84=EC=9C=BC=EB=A1=9C=20=EA=BA=BC?= =?UTF-8?q?=EB=82=B4=EC=98=A4=EB=8F=84=EB=A1=9D=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/com/techcourse/dao/UserDao.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 4343205b20..105f4a173a 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -12,10 +12,10 @@ public class UserDao { private static final Logger log = LoggerFactory.getLogger(UserDao.class); private static final RowMapper USER_ROW_MAPPER = rs -> new User( - rs.getLong(1), - rs.getString(2), - rs.getString(3), - rs.getString(4) + rs.getLong("id"), + rs.getString("account"), + rs.getString("password"), + rs.getString("email") ); private final JdbcTemplate jdbcTemplate; From 6dee2651ee8e5910b2b1a494c6f03546325fa1a7 Mon Sep 17 00:00:00 2001 From: devHudi Date: Wed, 5 Oct 2022 13:37:49 +0900 Subject: [PATCH 12/13] =?UTF-8?q?refactor:=20PreparedStatement=EC=97=90=20?= =?UTF-8?q?=ED=8C=8C=EB=9D=BC=EB=AF=B8=ED=84=B0=EB=A5=BC=20=EB=A7=A4?= =?UTF-8?q?=ED=95=91=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81=EC=9D=84=20?= =?UTF-8?q?=EB=A9=94=EC=86=8C=EB=93=9C=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java index 9fc78d30bd..1b5029e10f 100644 --- a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java +++ b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java @@ -48,11 +48,7 @@ public T usePreparedStatement(final String sql, final Object... args) { try (Connection connection = dataSource.getConnection(); PreparedStatement preparedStatement = connection.prepareStatement(sql)) { - - for (int i = 0; i < args.length; i++) { - preparedStatement.setObject(i + 1, args[i]); - } - + mapParametersToPreparedStatement(preparedStatement, args); return function.apply(preparedStatement); } catch (final SQLException e) { log.error("error: {}", e); @@ -60,4 +56,11 @@ public T usePreparedStatement(final String sql, // TODO: 더 추상화된 예외 메시지를 사용해야함 } } + + private void mapParametersToPreparedStatement(final PreparedStatement preparedStatement, final Object[] args) + throws SQLException { + for (int i = 0; i < args.length; i++) { + preparedStatement.setObject(i + 1, args[i]); + } + } } From 90ed5779c3a765974072f72bbfccbb312d403b88 Mon Sep 17 00:00:00 2001 From: devHudi Date: Wed, 5 Oct 2022 18:09:45 +0900 Subject: [PATCH 13/13] =?UTF-8?q?refactor:=20ResultSet=EC=9D=84=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=EB=A1=9C=20?= =?UTF-8?q?=EB=A7=A4=ED=95=91=ED=95=98=EB=8A=94=20=EB=A1=9C=EC=A7=81?= =?UTF-8?q?=EC=9D=84=20=EB=A9=94=EC=86=8C=EB=93=9C=EB=A1=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/nextstep/jdbc/JdbcTemplate.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java index 1b5029e10f..485da70ef8 100644 --- a/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java +++ b/jdbc/src/main/java/nextstep/jdbc/JdbcTemplate.java @@ -26,16 +26,18 @@ public int update(final String sql, final Object... args) { } public List query(final String sql, final RowMapper rowMapper, final Object... args) { - return usePreparedStatement(sql, pstmt -> { - List results = new ArrayList<>(); - ResultSet rs = pstmt.executeQuery(); - if (rs.next()) { - T result = rowMapper.run(rs); - results.add(result); - } + return usePreparedStatement(sql, pstmt -> mapToList(rowMapper, pstmt), args); + } + + private List mapToList(final RowMapper rowMapper, final PreparedStatement pstmt) throws SQLException { + List results = new ArrayList<>(); + ResultSet rs = pstmt.executeQuery(); + if (rs.next()) { + T result = rowMapper.run(rs); + results.add(result); + } - return results; - }, args); + return results; } public T queryForObject(final String sql, final RowMapper rowMapper, final Object... args) {