From 8b0e7576a41654da0ca1c5ff0dfd605f9a919982 Mon Sep 17 00:00:00 2001 From: thdwoqor Date: Tue, 26 Sep 2023 23:31:49 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20JdbcTemplate=20update=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 | 79 +++---------------- .../jdbc/core/JdbcTemplate.java | 35 ++++++++ 2 files changed, 45 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index 827d889166..b31501fd1b 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,9 +1,6 @@ package com.techcourse.dao; import com.techcourse.domain.User; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.SQLException; import java.util.List; import javax.sql.DataSource; import org.slf4j.Logger; @@ -28,77 +25,21 @@ public UserDao(final 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) { String sql = "update users set id = ?, account = ?, password = ?, email = ? where id = ?"; - Connection conn = null; - PreparedStatement pstmt = null; - - try { - conn = dataSource.getConnection(); - pstmt = conn.prepareStatement(sql); - - log.debug("query : {}", sql); - - pstmt.setLong(1, user.getId()); - pstmt.setString(2, user.getAccount()); - pstmt.setString(3, user.getPassword()); - pstmt.setString(4, user.getEmail()); - pstmt.setLong(5, user.getId()); - 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) { - } - } - + jdbcTemplate.update(sql, + user.getId(), + user.getAccount(), + user.getPassword(), + user.getEmail(), + user.getId() + ); } public List findAll() { 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 5e69340468..86efd404cf 100644 --- a/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java +++ b/jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java @@ -107,4 +107,39 @@ public List query(String sql, RowMapper rowMapper) { } } } + + public int update(String sql, Object... args) { + Connection conn = null; + PreparedStatement pstmt = null; + + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + + log.debug("query : {}", sql); + + for (int i = 0; i < args.length; i++) { + pstmt.setObject(i + 1, args[i]); + } + + return 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) { + } + } + } }