-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathUserDao.java
More file actions
77 lines (62 loc) · 2.53 KB
/
UserDao.java
File metadata and controls
77 lines (62 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package db;
import model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
private static final Logger logger = LoggerFactory.getLogger(UserDao.class);
// 회원가입
public void insert(User user) {
String sql = "INSERT INTO USERS (userId, name, password, email) VALUES (?, ?, ?, ?)";
try (Connection connection = ConnectionManager.getConnection();
PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, user.userId());
pstmt.setString(2, user.name());
pstmt.setString(3, user.password());
pstmt.setString(4, user.email());
pstmt.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Failed to save user", e);
}
}
// ID로 유저 정보 찾기
public User findUserById(String userId) {
String sql = "SELECT * FROM USERS WHERE userId = ?";
try (Connection connection = ConnectionManager.getConnection();
PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, userId);
try (ResultSet rs = pstmt.executeQuery()) {
if (rs.next()) {
return new User(
rs.getString("userId"),
rs.getString("password"),
rs.getString("name"),
rs.getString("email"),
rs.getString("profileImage")
);
}
}
} catch (SQLException e) {
throw new RuntimeException("Failed to find user", e);
}
return null;
}
// 정보 갱신
public void update(User user) {
String sql = "UPDATE USERS SET name = ?, email = ?, profileImage = ? WHERE userId = ?";
try (Connection connection = ConnectionManager.getConnection();
PreparedStatement pstmt = connection.prepareStatement(sql)) {
pstmt.setString(1, user.name());
pstmt.setString(2, user.email());
pstmt.setString(3, user.profileImage());
pstmt.setString(4, user.userId());
pstmt.executeUpdate();
logger.debug("User updated in DB: {}", user.userId());
} catch (SQLException e) {
logger.error("DB Update Error (User): {}", e.getMessage());
}
}
}