-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserDao.java
343 lines (334 loc) · 15.1 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package cn.itcast.accountinglearn.dao;
import cn.itcast.accountinglearn.model.KnowLedge;
import cn.itcast.accountinglearn.model.Situation;
import cn.itcast.accountinglearn.model.User;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
public class UserDao {
public User login (Connection con,User user)throws Exception{
User resultUser = null;
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con= DriverManager.getConnection(url,username,password);
String sql = "select * from user where username=? and password=?";
PreparedStatement pstmt =(PreparedStatement) con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
// pstmt.setString(5, user.getSex());
// pstmt.setString(6, user.getArea());
ResultSet rs = pstmt.executeQuery();
resultUser = new User();
if(rs.next()){
// resultUser.setUserId(rs.getInt("userid"));
resultUser.setUserName(rs.getString("username"));
resultUser.setSex(rs.getString("sex"));
resultUser.setRole(rs.getInt("role"));
resultUser.setArea(rs.getString("area"));
}
return resultUser;
}
//查询数据库中会计要素放入表格
public static List<User> getAllSituation()throws Exception{
List<User> list = new ArrayList<>();
Connection con =null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con= DriverManager.getConnection(url,username,password);
String sql =" select * from user";
PreparedStatement pst = con.prepareStatement(sql);
try{
User u =null;
rs=pst.executeQuery();
while(rs.next()){
u = new User();
int user_id =rs.getInt("user_id");
String username1 = rs.getString("username");
String password1 = rs.getString("password");
int role = rs.getInt("role");
String sex = rs.getString("sex");
String area = rs.getString("area");
u.setUserId(user_id);
u.setUserName(username1);
u.setPassword(password1);
u.setRole(role);
u.setSex(sex);
u.setArea(area);
list.add(u);
}
return list;
}
catch(SQLException throwables){
throwables.printStackTrace();
}
return list;
}
//添加信息
public static void adduser(User u) throws Exception {
Connection con = null;
//1.加载数据库的驱动到JVM
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con = DriverManager.getConnection(url,username,password);
//3.通过Connection对象获取PreparedStatement对象
String sql="INSERT INTO user(user_id,username,password,role,sex,area)VALUES (?,?,?,?,?,?)";
PreparedStatement pst = con.prepareStatement(sql);
pst.setInt(1,u.getUserId());
pst.setString(2,u.getUserName());
pst.setString(3,u.getPassword());
pst.setInt(4,u.getRole());
pst.setString(5, u.getSex());
pst.setString(6, u.getArea());
pst.executeUpdate();
}
//修改信息
public static int modifyuser(User u) throws Exception {
Connection con = null;
//1.加载数据库的驱动到JVM
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con = DriverManager.getConnection(url,username,password);
//3.通过Connection对象获取PreparedStatement对象
String sql = "UPDATE user SET username=?,password=?,role=?,sex=?,area=? WHERE user_id=? ";
PreparedStatement pst =(PreparedStatement) con.prepareStatement(sql);
pst.setInt(6,u.getUserId());
pst.setString(1,u.getUserName());
pst.setString(2,u.getPassword());
pst.setInt(3,u.getRole());
pst.setString(4,u.getSex());
pst.setString(5,u.getArea());
return pst.executeUpdate();
}
//删除学生信息
public static void deleteuser(User u) throws Exception {
Connection con = null;
//1.加载数据库的驱动到JVM
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con = DriverManager.getConnection(url, username, password);
//3.通过Connection对象获取PreparedStatement对象
String sql = "DELETE FROM user WHERE user_id=?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setInt(1,u.getUserId());
pst.executeUpdate();
}
public static User authenticateUser(String username, String password) {
User user = null;
String url= "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username1 = "root";
String Password = "123456";
try {
Connection connection = DriverManager.getConnection(url, username1, Password);
String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, username);
statement.setString(2, password);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next())
{ int user_id = resultSet.getInt("user_id");
String name = resultSet.getString("username");
String password1 = resultSet.getString("password");
int role = resultSet.getInt("role");
String gender = resultSet.getString("sex");
String area = resultSet.getString("area");
user = new User();
}
connection.close(); }
catch (SQLException e)
{ e.printStackTrace(); }
return user; }
//连接个人中心
public User connectionpersonal(User user)throws Exception{
Connection con = null;
//1.加载数据库的驱动到JVM
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con = DriverManager.getConnection(url, username, password);
User resultUser = null;
String sql = "select * from user where username=? and password=?";
PreparedStatement pstmt =(PreparedStatement) con.prepareStatement(sql);
pstmt.setString(1, user.getUserName());
pstmt.setString(2, user.getPassword());
// pstmt.setString(5, user.getSex());
// pstmt.setString(6, user.getArea());
ResultSet rs = pstmt.executeQuery();
resultUser = new User();
if(rs.next()){
// resultUser.setUserId(rs.getInt("userid"));
resultUser.setUserName(rs.getString("username"));
resultUser.setSex(rs.getString("sex"));
resultUser.setRole(rs.getInt("role"));
resultUser.setArea(rs.getString("area"));
}
return resultUser;
}
//获取密码
public static String getpassword(String user_id){
String password ="";
Connection connection = null;
PreparedStatement preparedStatement =null;
ResultSet resultSet = null;
try{
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password1 = "123456";
connection = DriverManager.getConnection(url, username, password);
String sql = "SELECT password FROM user WHERE user_id=?";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,user_id);
resultSet = preparedStatement.executeQuery();
if(resultSet.next()){
password = resultSet.getString("password");
resultSet.getInt("user_id" );
}else{
return null;
}
}catch(Exception e){
}
return password;
}
public static User getusers(User u){
Connection con = null;
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
User resultUser = new User();
try {
con = DriverManager.getConnection(url, username, password);
String sql = "SELECT * FROM user WHERE username = ? AND password = ?";
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, u.getUserName());
statement.setString(2, u.getPassword());
ResultSet resultSet = statement.executeQuery();
if(resultSet.next()){
// resultUser.setUserId(rs.getInt("userid"));
resultUser.setArea(resultSet.getString("area"));
}
resultUser.setArea(resultSet.getString("area"));
} catch (SQLException e) {
throw new RuntimeException(e);
}
return resultUser;
}
public static List<User> getFactor(int key)throws Exception{
List<User> list = new ArrayList<>();
Connection con =null;
ResultSet rs = null;
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con= DriverManager.getConnection(url,username,password);
String sql =" select * from user Where user_id=?";
PreparedStatement pst = con.prepareStatement(sql);
pst.setInt(1,key);
try{
User u =null;
rs=pst.executeQuery();
while(rs.next()){
u = new User();
int user_id =rs.getInt("user_id");
String username1 = rs.getString("username");
String password1 = rs.getString("password");
int role = rs.getInt("role");
String sex = rs.getString("sex");
String area = rs.getString("area");
u.setUserId(user_id);
u.setUserName(username1);
u.setPassword(password1);
u.setRole(role);
u.setSex(sex);
u.setArea(area);
list.add(u);
}
return list;
}
catch(SQLException throwables){
throwables.printStackTrace();
}
return list;
}
//修改个人姓名
public static int modifyname(User u) throws Exception {
Connection con = null;
//1.加载数据库的驱动到JVM
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con = DriverManager.getConnection(url,username,password);
//3.通过Connection对象获取PreparedStatement对象
String sql = "UPDATE user SET username=?WHERE user_id=? ";
PreparedStatement pst =(PreparedStatement) con.prepareStatement(sql);
pst.setInt(2,u.getUserId());
pst.setString(1,u.getUserName());
return pst.executeUpdate();
}
//修改密码
public static int modifypassword(User u) throws Exception {
Connection con = null;
//1.加载数据库的驱动到JVM
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con = DriverManager.getConnection(url,username,password);
//3.通过Connection对象获取PreparedStatement对象
String sql = "UPDATE user SET password=?WHERE user_id=? ";
PreparedStatement pst =(PreparedStatement) con.prepareStatement(sql);
pst.setInt(2,u.getUserId());
pst.setString(1,u.getPassword());
return pst.executeUpdate();
}
//修改性别
public static int modifysex(User u) throws Exception {
Connection con = null;
//1.加载数据库的驱动到JVM
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con = DriverManager.getConnection(url,username,password);
//3.通过Connection对象获取PreparedStatement对象
String sql = "UPDATE user SET sex=?WHERE user_id=? ";
PreparedStatement pst =(PreparedStatement) con.prepareStatement(sql);
pst.setInt(2,u.getUserId());
pst.setString(1,u.getSex());
return pst.executeUpdate();
}
//修改籍贯
public static int modifyarea(User u) throws Exception {
Connection con = null;
//1.加载数据库的驱动到JVM
Class.forName("com.mysql.cj.jdbc.Driver");
//2.获取连接
String url = "jdbc:mysql://localhost:3306/javaaccounting?serverTimezone=GMT%2B8 & useSSL=true & characterEncoding=utf8";
String username = "root";
String password = "123456";
con = DriverManager.getConnection(url,username,password);
//3.通过Connection对象获取PreparedStatement对象
String sql = "UPDATE user SET area=?WHERE user_id=? ";
PreparedStatement pst =(PreparedStatement) con.prepareStatement(sql);
pst.setInt(2,u.getUserId());
pst.setString(1,u.getArea());
return pst.executeUpdate();
}
}