-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathUserDao.java
More file actions
53 lines (43 loc) · 1.63 KB
/
UserDao.java
File metadata and controls
53 lines (43 loc) · 1.63 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
package db;
import model.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
// 회원가입
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")
);
}
}
} catch (SQLException e) {
throw new RuntimeException("Failed to find user", e);
}
return null;
}
}