Skip to content

Commit

Permalink
chore: try-catch-resource 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
donghae-kim committed Sep 26, 2023
1 parent e2fc3e5 commit 64e8c45
Showing 1 changed file with 6 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public JdbcTemplate(final DataSource dataSource) {
public void update(final String sql, final Object... objects) {
try (final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql)) {

log.debug("query : {}", sql);
for (int i = 0; i < objects.length; i++) {
pstmt.setObject(i + 1, objects[i]);
Expand All @@ -39,16 +40,14 @@ public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, fina
try (final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql)) {

log.debug("query : {}", sql);
for (int i = 0; i < objects.length; i++) {
pstmt.setObject(i + 1, objects[i]);
}
ResultSet rs = pstmt.executeQuery();
log.debug("query : {}", sql);

final ResultSet rs = pstmt.executeQuery();
if (rs.next()) {
return rowMapper.execute(rs);
}

return null;
} catch (SQLException e) {
log.error(e.getMessage(), e);
Expand All @@ -58,16 +57,14 @@ public <T> T queryForObject(final String sql, final RowMapper<T> rowMapper, fina

public <T> List<T> query(final String sql, final RowMapper<T> rowMapper) {
try (final Connection conn = dataSource.getConnection();
final PreparedStatement pstmt = conn.prepareStatement(sql)) {

final List<T> results = new ArrayList<>();
final PreparedStatement pstmt = conn.prepareStatement(sql);
final ResultSet rs = pstmt.executeQuery()) {

log.debug("query : {}", sql);
ResultSet rs = pstmt.executeQuery();
final List<T> results = new ArrayList<>();
while (rs.next()) {
results.add(rowMapper.execute(rs));
}

return results;
} catch (SQLException e) {
log.error(e.getMessage(), e);
Expand Down

0 comments on commit 64e8c45

Please sign in to comment.