From 7619252eab7f51acb4261306ba1b4295ea0376a5 Mon Sep 17 00:00:00 2001 From: kevstevie Date: Tue, 26 Sep 2023 14:57:36 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20dao=20=EA=B5=AC=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 | 105 +++++++++++++++++- 1 file changed, 100 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/com/techcourse/dao/UserDao.java b/app/src/main/java/com/techcourse/dao/UserDao.java index d14c545f34..252b68fb7a 100644 --- a/app/src/main/java/com/techcourse/dao/UserDao.java +++ b/app/src/main/java/com/techcourse/dao/UserDao.java @@ -1,6 +1,7 @@ package com.techcourse.dao; import com.techcourse.domain.User; +import java.util.ArrayList; import org.springframework.jdbc.core.JdbcTemplate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,12 +61,65 @@ public void insert(final User user) { } public void update(final User user) { - // todo + final var sql = "update users set account = ?, email = ?, password = ? where id = ?"; + + 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.getEmail()); + pstmt.setString(3, user.getPassword()); + pstmt.setLong(4, 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) {} + } + } public List findAll() { - // todo - return null; + final String sql = "select * from users"; + List users = new ArrayList<>(); + Connection conn; + PreparedStatement pstmt; + ResultSet rs; + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + rs = pstmt.executeQuery(); + + log.debug("query : {}", sql); + + while (rs.next()) { + users.add(new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4))); + } + return users; + } catch (SQLException e) { + log.error(e.getMessage(), e); + throw new RuntimeException(e); + } } public User findById(final Long id) { @@ -115,7 +169,48 @@ public User findById(final Long id) { } public User findByAccount(final String account) { - // todo - return null; + final var sql = "select id, account, password, email from users where account = ?"; + + Connection conn = null; + PreparedStatement pstmt = null; + ResultSet rs = null; + try { + conn = dataSource.getConnection(); + pstmt = conn.prepareStatement(sql); + pstmt.setString(1, account); + rs = pstmt.executeQuery(); + + log.debug("query : {}", sql); + + if (rs.next()) { + return new User( + rs.getLong(1), + rs.getString(2), + rs.getString(3), + rs.getString(4)); + } + return null; + } catch (SQLException e) { + log.error(e.getMessage(), e); + throw new RuntimeException(e); + } finally { + try { + if (rs != null) { + rs.close(); + } + } catch (SQLException ignored) {} + + try { + if (pstmt != null) { + pstmt.close(); + } + } catch (SQLException ignored) {} + + try { + if (conn != null) { + conn.close(); + } + } catch (SQLException ignored) {} + } } }