-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserDAO.java
More file actions
274 lines (254 loc) · 8.29 KB
/
Copy pathUserDAO.java
File metadata and controls
274 lines (254 loc) · 8.29 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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Model;
import data.UserDTO;
import Utils.DBUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author thien
*/
public class UserDAO {
public UserDTO checkLogin(String email, String password) throws SQLException {
UserDTO user = null;
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs = null;
String sql = "";
try {
conn = DBUtils.getConnection();
if (conn != null) {
sql = "SELECT * from tblCustomer"
+ " where email=?"
+ " and password=? and status = 1";
stm = conn.prepareStatement(sql);
stm.setString(1, email);
stm.setString(2, password);
rs = stm.executeQuery();
if (rs.next()) {
String name = rs.getString("name");
String userId = rs.getString("id");
String role = rs.getString("role");
user = new UserDTO(userId, name, email, "*****", role);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (conn != null) {
conn.close();
}
}
return user;
}
public boolean creatUser(String name, String email, String password) throws SQLException {
boolean result = false;
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
String sql = " INSERT INTO tblCustomer (email, password, name, role,status) VALUES (?,?,?,'US',1)";
stm = conn.prepareStatement(sql);
stm.setString(1, email);
stm.setString(2, password);
stm.setString(3, name);
int value = stm.executeUpdate();
result = value > 0 ? true : false;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (conn != null) {
conn.close();
}
}
return result;
}
public UserDTO load(int id) throws SQLException {
List<UserDTO> listCopy = getListUser("", true);
for (UserDTO studentDTO : listCopy) {
if (studentDTO.getUserID().equals(Integer.toString(id))) {
return studentDTO;
}
}
return null;
}
public int updateUser(UserDTO user) throws SQLException {
int value = 0;
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
String sql = "UPDATE tblCustomer SET "
+ " name = ? , role = ? WHERE id = ?;";
stm = conn.prepareStatement(sql);
stm.setString(2, user.getRole());
stm.setInt(3, Integer.parseInt(user.getUserID()));
stm.setString(1, user.getName());
value = stm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (conn != null) {
conn.close();
}
}
return value;
}
public int updateUserInfo(UserDTO user) throws SQLException {
int value = 0;
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
String sql = "UPDATE tblCustomer SET "
+ " name = ? , password = ? WHERE id = ?;";
stm = conn.prepareStatement(sql);
stm.setInt(3, Integer.parseInt(user.getUserID()));
stm.setString(2, user.getPassword() );
stm.setString(1, user.getName());
value = stm.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (conn != null) {
conn.close();
}
}
return value;
}
public boolean delete(String userID) throws SQLException {
boolean value = false;
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
String sql = "Update tblCustomer set "
+ " status = 0 where id = ?";
stm = conn.prepareStatement(sql);
stm.setInt(1, Integer.parseInt(userID));
value = stm.executeUpdate() > 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (conn != null) {
conn.close();
}
}
return value;
}
public boolean restore(String userID) throws SQLException {
boolean value = false;
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
String sql = "Update tblCustomer set "
+ " status = 1 where id = ?";
stm = conn.prepareStatement(sql);
stm.setInt(1, Integer.parseInt(userID));
value = stm.executeUpdate() > 0;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (conn != null) {
conn.close();
}
}
return value;
}
public List<UserDTO> getListUser(String search, boolean status) throws SQLException {
List<UserDTO> list = new ArrayList<>();
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs = null;
try {
conn = DBUtils.getConnection();
if (conn != null) {
String sql = "SELECT * "
+ "from tblCustomer"
+ " Where (name LIKE ? or email like ? ) and status = ? and role = ?";// nhớ có dấu cách
stm = conn.prepareStatement(sql);
stm.setString(1, "%" + search + "%");
stm.setString(2, "%" + search + "%");
stm.setBoolean(3, status);
stm.setString(4,"US");
rs = stm.executeQuery();
while (rs.next()) {
String userID = rs.getString("id");
String fullname = rs.getString("name");
String email = rs.getString("email");
String password = "***";
String role = rs.getString("role");
UserDTO searchUser = new UserDTO(userID, fullname, email, password, role);
list.add(searchUser);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
rs.close();
}
if (stm != null) {
stm.close();
}
if (conn != null) {
conn.close();
}
}
return list;
}
public static void main(String[] args) throws SQLException {
UserDAO dao = new UserDAO();
}
}