Skip to content

Commit 7dc4804

Browse files
authored
[2단계 - 리팩터링] 링크(손준형) 미션 제출합니다. (#959)
* test: update test * refactor: isolation of methods and interfaces * refactor: delete unused annotation * refactor: handle resource leak
1 parent 0f2b484 commit 7dc4804

File tree

4 files changed

+57
-11
lines changed

4 files changed

+57
-11
lines changed

app/src/test/java/com/techcourse/dao/UserDaoTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ void findById() {
3737

3838
@Test
3939
void findByAccount() {
40-
final var account = "gugu";
40+
final var account = "link";
41+
final var password = "password";
42+
final var email = " [email protected]";
43+
userDao.insert(new User(account, password, email));
44+
4145
final var user = userDao.findByAccount(account);
4246

4347
assertThat(user.getAccount()).isEqualTo(account);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.interface21.jdbc.core;
2+
3+
import java.sql.PreparedStatement;
4+
import java.sql.SQLException;
5+
6+
public class ArgumentPreparedStatementSetter implements PreparedStatementSetter {
7+
private final Object[] args;
8+
9+
public ArgumentPreparedStatementSetter(Object[] args) {
10+
this.args = args;
11+
}
12+
13+
@Override
14+
public void setValues(PreparedStatement pstmt) throws SQLException {
15+
for (int idx = 1; idx <= args.length; idx++) {
16+
pstmt.setObject(idx, args[idx - 1]);
17+
}
18+
}
19+
}

jdbc/src/main/java/com/interface21/jdbc/core/JdbcTemplate.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ public JdbcTemplate(final DataSource dataSource) {
4242
* @return 제네릭 타입 결과값 (쿼리 결과나 update 결과 등)
4343
*/
4444
public <R> R execute(String sql, Object[] args, StatementExecutor<R> executor) {
45-
try (Connection conn = dataSource.getConnection();
46-
PreparedStatement pstmt = conn.prepareStatement(sql)) {
47-
45+
try (
46+
Connection conn = getConnection();
47+
PreparedStatement pstmt = conn.prepareStatement(sql)
48+
) {
4849
setPreparedStatementParameter(args, pstmt);
4950
log.info("query = {}", sql);
5051

@@ -54,6 +55,18 @@ public <R> R execute(String sql, Object[] args, StatementExecutor<R> executor) {
5455
}
5556
}
5657

58+
/**
59+
* PreparedStatement에 파라미터를 순서대로 바인딩하는 내부 메서드.
60+
*
61+
* @param args SQL ? 에 바인딩할 파라미터
62+
* @param pstmt PreparedStatement 객체
63+
* @throws SQLException SQL 실행 중 발생한 예외
64+
*/
65+
private void setPreparedStatementParameter(Object[] args, PreparedStatement pstmt) throws SQLException {
66+
final PreparedStatementSetter pss = new ArgumentPreparedStatementSetter(args);
67+
pss.setValues(pstmt);
68+
}
69+
5770
/**
5871
* INSERT, UPDATE, DELETE 문과 같이 데이터 변경 쿼리를 실행하는 메서드.
5972
* 내부적으로 execute()를 호출하여 PreparedStatement.executeUpdate()를 수행한다.
@@ -120,15 +133,16 @@ private <T> List<T> getQueryResult(RowMapper<T> rowMapper, PreparedStatement pst
120133
}
121134

122135
/**
123-
* PreparedStatement에 파라미터를 순서대로 바인딩하는 내부 메서드.
136+
* DataSource에서 새로운 데이터베이스 Connection 객체 획득
124137
*
125-
* @param args SQL ? 에 바인딩할 파라미터
126-
* @param pstmt PreparedStatement 객체
127-
* @throws SQLException SQL 실행 중 발생한 예외
138+
* @return 데이터베이스에 연결된 Connection 객체
139+
* @throws DataAccessException 커넥션 획득 과정에서 SQL 오류가 발생한 경우
128140
*/
129-
private void setPreparedStatementParameter(Object[] args, PreparedStatement pstmt) throws SQLException {
130-
for (int idx = 1; idx <= args.length; idx++) {
131-
pstmt.setObject(idx, args[idx - 1]);
141+
private Connection getConnection() {
142+
try {
143+
return dataSource.getConnection();
144+
} catch (SQLException e) {
145+
throw new DataAccessException(e);
132146
}
133147
}
134148
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.interface21.jdbc.core;
2+
3+
import java.sql.PreparedStatement;
4+
import java.sql.SQLException;
5+
6+
@FunctionalInterface
7+
public interface PreparedStatementSetter {
8+
void setValues(PreparedStatement pstmt) throws SQLException;
9+
}

0 commit comments

Comments
 (0)