Repository navigation
Expand file tree
/
Copy pathUserDao.java
More file actions
70 lines (61 loc) · 1.5 KB
/
Copy pathUserDao.java
File metadata and controls
70 lines (61 loc) · 1.5 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
package next.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import core.jdbc.ConnectionManager;
import next.model.User;
public class UserDao {
public void insert(User user) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
try {
con = ConnectionManager.getConnection();
String sql = "INSERT INTO USERS VALUES (?, ?, ?, ?)";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, user.getUserId());
pstmt.setString(2, user.getPassword());
pstmt.setString(3, user.getName());
pstmt.setString(4, user.getEmail());
pstmt.executeUpdate();
} finally {
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
}
}
public User findByUserId(String userId) throws SQLException {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
con = ConnectionManager.getConnection();
String sql = "SELECT userId, password, name, email FROM USERS WHERE userid=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, userId);
rs = pstmt.executeQuery();
User user = null;
if (rs.next()) {
user = new User(
rs.getString("userId"),
rs.getString("password"),
rs.getString("name"),
rs.getString("email"));
}
return user;
} finally {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
if (con != null) {
con.close();
}
}
}
}