From 64e8c45eb985be794ab7a0151b7ccbdb27d41fab Mon Sep 17 00:00:00 2001 From: donghae-kim Date: Tue, 26 Sep 2023 23:47:20 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20try-catch-resource=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../springframework/jdbc/core/JdbcTemplate.java | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java index 842fea6e56..12e63ea8d2 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -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]); @@ -39,16 +40,14 @@ public T queryForObject(final String sql, final RowMapper 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); @@ -58,16 +57,14 @@ public T queryForObject(final String sql, final RowMapper rowMapper, fina public List query(final String sql, final RowMapper rowMapper) { try (final Connection conn = dataSource.getConnection(); - final PreparedStatement pstmt = conn.prepareStatement(sql)) { - - final List results = new ArrayList<>(); + final PreparedStatement pstmt = conn.prepareStatement(sql); + final ResultSet rs = pstmt.executeQuery()) { log.debug("query : {}", sql); - ResultSet rs = pstmt.executeQuery(); + final List results = new ArrayList<>(); while (rs.next()) { results.add(rowMapper.execute(rs)); } - return results; } catch (SQLException e) { log.error(e.getMessage(), e);