/
UserDao.java
160 lines (143 loc) · 4.92 KB
/
UserDao.java
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package tobystudy.group.template.v4;
import org.springframework.dao.EmptyResultDataAccessException;
import tobystudy.group.template.User;
import tobystudy.group.template.v3.StatementStrategy;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDao {
//복잡한 try catch finally
private final JdbcContext jdbcConnection;
private final ConnectionMaker connectionMaker;
public UserDao(JdbcContext jdbcConnection, ConnectionMaker connectionMaker) {
this.jdbcConnection = jdbcConnection;
this.connectionMaker = connectionMaker;
}
public void add(final User user) throws SQLException {
this.jdbcConnection.workWithStatementStrategy(c -> {
PreparedStatement stmt = c.prepareStatement("insert into users(id,name,password) values(?,?,?)");
stmt.setString(1, user.getId());
stmt.setString(2, user.getName());
stmt.setString(3, user.getPassword());
return stmt;
});
}
public void deleteAll() throws SQLException {
this.jdbcConnection.executeUpdate("delete from users");
}
public int getCount() {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = connectionMaker.makeConnection();
stmt = conn.prepareStatement("select count(*) from users");
resultSet = stmt.executeQuery();
if (resultSet.next()) {
return resultSet.getInt(1);
}
return 0;
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
public User get(String id) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet resultSet = null;
try {
conn = connectionMaker.makeConnection();
stmt = conn.prepareStatement("select * from users where id = ?");
stmt.setString(1, id);
resultSet = stmt.executeQuery();
User user = null;
if (resultSet.next()) {
resultSet.getString(1);
user = User.builder().id(resultSet.getString(1)).name(resultSet.getString(2)).password(resultSet.getString(3)).build();
}
if (user == null) {
throw new EmptyResultDataAccessException("해당 유저가 존재하지않음", 0);
}
return user;
} catch (SQLException e) {
throw new RuntimeException(e);
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
public void jdbcContextWithStatementStrategy(StatementStrategy stmt) throws SQLException {
Connection c = null;
PreparedStatement ps = null;
try {
c = connectionMaker.makeConnection();
ps = stmt.makePreparedStatement(c);
ps.executeUpdate();
} catch (SQLException e) {
throw e;
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} finally {
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
if (c != null) {
try {
c.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
}
}