Skip to content

Commit

Permalink
feat: dao 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kevstevie committed Sep 26, 2023
1 parent 7182a49 commit 7619252
Showing 1 changed file with 100 additions and 5 deletions.
105 changes: 100 additions & 5 deletions app/src/main/java/com/techcourse/dao/UserDao.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<User> findAll() {
// todo
return null;
final String sql = "select * from users";
List<User> 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) {
Expand Down Expand Up @@ -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) {}
}
}
}

0 comments on commit 7619252

Please sign in to comment.