Skip to content

Commit

Permalink
feat: JdbcTemplate update 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
thdwoqor committed Sep 26, 2023
1 parent 362b083 commit 8b0e757
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 69 deletions.
79 changes: 10 additions & 69 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<User> findAll() {
Expand Down
35 changes: 35 additions & 0 deletions jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,39 @@ public <T> List<T> query(String sql, RowMapper<T> 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) {
}
}
}
}

0 comments on commit 8b0e757

Please sign in to comment.