Skip to content

Commit

Permalink
refactor: JdbcTemplate에 PreparedStatement의 파라미터를 설정하는 중복 코드 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
kwonyj1022 committed Sep 26, 2023
1 parent 182889d commit 343b1f9
Showing 1 changed file with 24 additions and 21 deletions.
45 changes: 24 additions & 21 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ public int update(final String sql, final Object... args) {
) {
log.debug("query : {}", sql);

final int argsLength = args.length;
for (int i = 0; i < argsLength; i++) {
pstmt.setObject(i + 1, args[i]);
}
processPreparedStatementParameter(pstmt, args);

return pstmt.executeUpdate();
} catch (SQLException e) {
Expand All @@ -40,15 +37,19 @@ public int update(final String sql, final Object... args) {
}
}

private void processPreparedStatementParameter(final PreparedStatement pstmt, final Object[] args) throws SQLException {
final int argsLength = args.length;
for (int i = 0; i < argsLength; i++) {
pstmt.setObject(i + 1, args[i]);
}
}

public <T> List<T> query(final String sql, final RowMapper<T> rowMaper, final Object... args) {
try (Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
log.debug("query : {}", sql);

final int argsLength = args.length;
for (int i = 0; i < argsLength; i++) {
pstmt.setObject(i + 1, args[i]);
}
processPreparedStatementParameter(pstmt, args);

List<T> results = new ArrayList<>();
try (ResultSet rs = pstmt.executeQuery()) {
Expand All @@ -67,26 +68,18 @@ public <T> List<T> query(final String sql, final RowMapper<T> rowMaper, final Ob
public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, final Object... args) {
try (Connection conn = dataSource.getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
final int argsLength = args.length;
for (int i = 0; i < argsLength; i++) {
pstmt.setObject(i + 1, args[i]);
}
log.debug("query : {}", sql);

try (ResultSet rs = pstmt.executeQuery()) {
processPreparedStatementParameter(pstmt, args);

log.debug("query : {}", sql);
try (ResultSet rs = pstmt.executeQuery()) {

T findObject = null;
if (rs.next()) {
findObject = rowMapper.mapRow(rs, rs.getRow());
}
if (findObject == null) {
throw new IncorrectResultSizeDataAccessException(0);
}
if (rs.next()) {
rs.last();
throw new IncorrectResultSizeDataAccessException(rs.getRow());
}

validateSingleResult(findObject, rs);

return findObject;
}
Expand All @@ -95,4 +88,14 @@ public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, fina
throw new RuntimeException(e);
}
}

private <T> void validateSingleResult(final T findObject, final ResultSet rs) throws SQLException {
if (findObject == null) {
throw new IncorrectResultSizeDataAccessException(0);
}
if (rs.next()) {
rs.last();
throw new IncorrectResultSizeDataAccessException(rs.getRow());
}
}
}

0 comments on commit 343b1f9

Please sign in to comment.