数据访问接口实现类CollegeDaoImpl
/**
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import net.xmh.student.bean.College;
import net.xmh.student.bean.dao.impl.CollegeDao;
import net.xmh.student.bean.dbutil.ConnectionManager;
public class CollegeDaoImpl implements CollegeDao {
@Override
public College findById(int id) {
// 声明学校对象
College college = null;
// 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "SELECT * FROM t_college WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setInt(1, id);
// 执行SQL查询,返回结果集
ResultSet rs = pstmt.executeQuery();
// 判断结果集是否有记录
if (rs.next()) {
// 实例化学校
college = new College();
// 利用当前记录字段值去设置学校对象的属性
college.setId(rs.getInt("id"));
college.setName(rs.getString("name"));
college.setPresident(rs.getString("president"));
college.setStartTime(rs.getDate("start_time"));
college.setTelephone(rs.getString("telephone"));
college.setEmail(rs.getString("email"));
college.setAddress(rs.getString("address"));
college.setProfile(rs.getString("profile"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回学校对象
return college;
}
@Override
public int update(College college) {
// 定义更新记录数
int count = 0;
// 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 定义SQL字符串
String strSQL = "UPDATE t_college SET name = ?, president = ?, start_time = ?,"
+ " telephone = ?, email = ?, address = ?, profile = ? WHERE id = ?";
try {
// 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 设置占位符的值
pstmt.setString(1, college.getName());
pstmt.setString(2, college.getPresident());
pstmt.setTimestamp(3, new Timestamp(college.getStartTime().getTime()));
pstmt.setString(4, college.getTelephone());
pstmt.setString(5, college.getEmail());
pstmt.setString(6, college.getAddress());
pstmt.setString(7, college.getProfile());
pstmt.setInt(8, college.getId());
// 执行更新操作,更新记录
count = pstmt.executeUpdate();
// 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
}
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
创建net.xmh.student.test包,在里面创建测试类TestCollegeDaoImpl
package net.xmh.student.bean.test;
import net.xmh.student.bean.College;
import net.xmh.student.bean.dao.impl.CollegeDao;
import net.xmh.student.bean.dao.CollegeDaoImpl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
/**
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
运行结果为:
状态数据访问接口实现类StatusDaoImpl
package net.xmh.student.bean.dao;
import net.xmh.student.bean.Status;
import net.xmh.student.bean.dbutil.ConnectionManager;
import net.xmh.student.bean.dao.impl.StatusDao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
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
在net.xmh.student.test包里创建测试类TestStatusDaoImpl
package net.xmh.student.bean.test;
import net.xmh.student.bean.Status;
import net.xmh.student.bean.dao.StatusDaoImpl;
import net.xmh.student.bean.dao.impl.StatusDao;
import org.junit.Test;
import javax.swing.plaf.nimbus.State;
/**
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
运行结果为:
学生数据访问接口实现类StudentDaoImpl
package net.xmh.student.bean.dao;
import net.xmh.student.bean.Student;
import net.xmh.student.bean.dao.impl.StudentDao;
import net.xmh.student.bean.dbutil.ConnectionManager;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
/**
功能:学生数据访问接口实现类
作者:华卫
日期:2019年6月18日
/
public class StudentDaoImpl implements StudentDao {
/*
插入学生记录
@param student
@return 插入记录数
*/
@Override
public int insert(Student student) {
// 定义插入记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “insert into t_student (id, name, sex, age, department, class, telephone)”
+ " values (?, ?, ?, ?, ?, ?, ?)";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, student.getId());
pstmt.setString(2, student.getName());
pstmt.setString(3, student.getSex());
pstmt.setInt(4, student.getAge());
pstmt.setString(5, student.getDepartment());
pstmt.setString(6, student.getClazz());
pstmt.setString(7, student.getTelephone());
// 5. 执行SQL,返回插入记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回插入记录数
return count;
}
/**
按学号删除学生记录
@param id
@return 删除记录数
*/
@Override
public int deleteById(String id) {
// 定义删除记录数
int count = 0;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “delete from t_student where id = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, id);
// 5. 执行SQL,返回删除记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
按班级删除学生记录
@param clazz
@return 删除记录数
*/
@Override
public int deleteByClass(String clazz) {
// 定义删除记录数
int count = 0;
// 1. 获取数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “delete from t_student where class = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, clazz);
// 5. 执行SQL,返回删除记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
按系部删除学生记录
@param department
@return 删除记录数
*/
@Override
public int deleteByDepartment(String department) {
// 定义删除记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “delete from t_student where department = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, department);
// 5. 执行SQL,返回删除记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回删除记录数
return count;
}
/**
更新学生记录
@param student
@return 更新记录数
*/
@Override
public int update(Student student) {
// 定义更新记录数
int count = 0;
// 1. 获得数据库连接
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “update t_student set name = ?, sex = ?, age = ?,”
+ " department = ?, class = ?, telephone = ? where id = ?";
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, student.getName());
pstmt.setString(2, student.getSex());
pstmt.setInt(3, student.getAge());
pstmt.setString(4, student.getDepartment());
pstmt.setString(5, student.getClazz());
pstmt.setString(6, student.getTelephone());
pstmt.setString(7, student.getId());
// 5. 执行SQL,返回更新记录数
count = pstmt.executeUpdate();
// 6. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回更新记录数
return count;
}
/**
按学号查询学生记录
@param id
@return 学生实体
*/
@Override
public Student findById(String id) {
// 声明学生对象
Student student = null;
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student where id = ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, id);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 判断结果集是否有记录
if (rs.next()) {
// 创建学生实体
student = new Student();
// 利用当前记录各字段值设置学生实体属性
student.setId(rs.getString(“id”));
student.setName(rs.getString(“name”));
student.setSex(rs.getString(“sex”));
student.setAge(rs.getInt(“age”));
student.setDepartment(rs.getString(“department”));
student.setClazz(rs.getString(“class”));
student.setTelephone(rs.getString(“telephone”));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生对象
return student;
}
/**
按姓名查询学生记录
@param name
@return 学生列表
*/
@Override
public List findByName(String name) {
// 声明学生列表
List students = new ArrayList();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student where name like ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, name + “%”);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 利用当前记录各字段值设置学生实体属性
student.setId(rs.getString(“id”));
student.setName(rs.getString(“name”));
student.setSex(rs.getString(“sex”));
student.setAge(rs.getInt(“age”));
student.setDepartment(rs.getString(“department”));
student.setClazz(rs.getString(“class”));
student.setTelephone(rs.getString(“telephone”));
// 将实体添加到学生列表
students.add(student);
}
// 7. 关闭结果集
rs.close();
// 8. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
按班级查询学生记录
@param clazz
@return 学生列表
*/
@Override
public List findByClass(String clazz) {
// 声明学生列表
List students = new ArrayList();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student where class like ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, clazz + “%”);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 利用当前记录各字段值设置学生实体属性
student.setId(rs.getString(“id”));
student.setName(rs.getString(“name”));
student.setSex(rs.getString(“sex”));
student.setAge(rs.getInt(“age”));
student.setDepartment(rs.getString(“department”));
student.setClazz(rs.getString(“class”));
student.setTelephone(rs.getString(“telephone”));
// 将实体添加到学生列表
students.add(student);
}
// 7. 关闭结果集
rs.close();
// 8. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
按系部查询学生记录
@param department
@return 学生列表
*/
@Override
public List findByDepartment(String department) {
// 声明学生列表
List students = new ArrayList();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student where department like ?”;
try {
// 3. 创建预备语句对象
PreparedStatement pstmt = conn.prepareStatement(strSQL);
// 4. 设置占位符的值
pstmt.setString(1, department + “%”);
// 5. 执行SQL,返回结果集
ResultSet rs = pstmt.executeQuery();
// 6. 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 利用当前记录各字段值设置学生实体属性
student.setId(rs.getString(“id”));
student.setName(rs.getString(“name”));
student.setSex(rs.getString(“sex”));
student.setAge(rs.getInt(“age”));
student.setDepartment(rs.getString(“department”));
student.setClazz(rs.getString(“class”));
student.setTelephone(rs.getString(“telephone”));
// 将实体添加到学生列表
students.add(student);
}
// 7. 关闭结果集
rs.close();
// 8. 关闭预备语句对象
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
查询全部学生记录
@return 学生列表
*/
@Override
public List findAll() {
// 声明学生列表
List students = new ArrayList();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select * from t_student”;
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 创建学生实体
Student student = new Student();
// 利用当前记录各字段值设置学生实体属性
student.setId(rs.getString(“id”));
student.setName(rs.getString(“name”));
student.setSex(rs.getString(“sex”));
student.setAge(rs.getInt(“age”));
student.setDepartment(rs.getString(“department”));
student.setClazz(rs.getString(“class”));
student.setTelephone(rs.getString(“telephone”));
// 将实体添加到学生列表
students.add(student);
}
// 6. 关闭结果集
rs.close();
// 7. 关闭语句对象
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回学生列表
return students;
}
/**
按性别统计学生人数
@return 统计结果向量
*/
@Override
public Vector findRowsBySex() {
// 定义行集向量
Vector rows = new Vector();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select sex as ‘性别’, count(*) as ‘人数’”
+ " from t_student group by sex order by sex desc";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 定义当前行向量
Vector currentRow = new Vector();
// 利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString(“性别”));
currentRow.addElement(rs.getInt(“人数”) + “”);
// 将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回行集向量
return rows;
}
/**
按班级统计学生人数
@return 统计结果向量
*/
@Override
public Vector findRowsByClass() {
// 定义行集向量
Vector rows = new Vector();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select class as ‘班级’, count(*) as ‘人数’”
+ " from t_student group by class order by class desc";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 定义当前行向量
Vector currentRow = new Vector();
// 利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString(“班级”));
currentRow.addElement(rs.getInt(“人数”) + “”);
// 将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回行集向量
return rows;
}
/**
按系部统计学生人数
@return 统计结果向量
*/
@Override
public Vector findRowsByDepartment() {
// 定义行集向量
Vector rows = new Vector();
// 1. 获取数据库连接对象
Connection conn = ConnectionManager.getConnection();
// 2. 定义SQL字符串
String strSQL = “select department as ‘系部’, count(*) as ‘人数’”
+ " from t_student group by department order by department desc";
try {
// 3. 创建语句对象
Statement stmt = conn.createStatement();
// 4. 执行SQL,返回结果集
ResultSet rs = stmt.executeQuery(strSQL);
// 5. 遍历结果集
while (rs.next()) {
// 定义当前行向量
Vector currentRow = new Vector();
// 利用当前记录字段值设置当前行向量的元素值
currentRow.addElement(rs.getString(“系部”));
currentRow.addElement(rs.getInt(“人数”) + “”);
// 将当前行向量添加到行集向量
rows.addElement(currentRow);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭数据库连接
ConnectionManager.closeConnection(conn);
}
// 返回行集向量
return rows;
}
}
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
在net.xmh.student.test包里创建测试类TestStudentDaoImpl
package net.xmh.student.bean.test;
import net.xmh.student.bean.Student;
import net.xmh.student.bean.dao.StudentDaoImpl;
import net.xmh.student.bean.dao.impl.StudentDao;
import org.junit.Test;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
/**
功能:测试学生数据访问接口实现类
作者:ygf
日期:2019年6月17日
*/
public class TestStudentDaoImpl {
@Test
public void testInsert(){
Student student = new Student();
student.setId(“12345678”);
student.setName(“琳琳”);
student.setSex(“女”);
student.setAge(19);
student.setDepartment(“信息工程系”);
student.setClazz(“18大数据1班”);
student.setTelephone(“78927657265”);
StudentDao dao = new StudentDaoImpl();
int count = dao.insert(student);
if (count > 0){
System.out.println("恭喜,学生记录插入成功!");
}else {
System.out.println("遗憾,学生记录插入失败!");
}
}
@Test
public void testDeleteById(){
StudentDao dao = new StudentDaoImpl();
String id = “12345678”;
int count = dao.deleteById(id);
if (count > 0 ){
System.out.println(“恭喜,学生记录删除成功!”);
}else {
System.out.println(“遗憾,学生记录删除失败!”);
}
}
@Test
public void testDeleteByClass(){
StudentDao dao = new StudentDaoImpl();
String clazz = “11英教1班”;
int count = dao.deleteByClass(clazz);
if (count > 0){
System.out.println(“恭喜,[” +clazz +"]学生记录删除成功!");
}else {
System.out.println(“遗憾,[” + clazz+"]学生记录删除失败!");
}
}
@Test
public void deleteByDepartment(){
StudentDao dao = new StudentDaoImpl();
String department = “信息工程系”;
int count = dao.deleteByDepartment(department);
if (count > 0){
System.out.println(“恭喜,[” +department+"]学生记录删除成功!");
}else {
System.out.println(“遗憾,[” +department+"]学生记录删除失败!");
}
}
@Test
public void testUpdate() {
StudentDao dao = new StudentDaoImpl();
Student student = dao.findById(“10080301”);
student.setName(“圆圆”);
int count = dao.update(student);
if (count > 0){
System.out.println(“更新成功!”);
}else{
System.out.println(“更新失败!”);
}
}
@Test
public void testFindByName(){
StudentDao dao = new StudentDaoImpl();
String name = “李”;
List students = dao.findByName(name);
for (Student student:students){
System.out.println(student);
}
}
@Test
public void testFindById(){
StudentDao dao = new StudentDaoImpl();
Student student = dao.findById(“10080301”);
System.out.println(student);
}
@Test
public void testFindByClass() {
StudentDao dao = new StudentDaoImpl();
String clazz = “10英教1班”;
List students = dao.findByClass(clazz);
for (Student student : students) {
System.out.println(student);
}
}
@Test
public void testFindByDepartment(){
StudentDao dao = new StudentDaoImpl();
String department = “外语系”;
List students = dao.findByDepartment(department);
for (Student student : students){
System.out.println(student);
}
}
@Test
public void FindAll(){
StudentDao dao = new StudentDaoImpl();
String all;
List students = dao.findAll();
for (Student student : students){
System.out.println(student);
}
}
@Test
public void testFindRowsByClass(){
StudentDao dao = new StudentDaoImpl();
Vector rows = dao.findRowsByClass();
Iterator iterator = rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsByDepartment(){
StudentDao dao = new StudentDaoImpl();
Vector rows = dao.findRowsByDepartment();
Iterator iterator = rows.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
@Test
public void testFindRowsBySex() {
StudentDao dao = new StudentDaoImpl();
Vector rows = dao.findRowsBySex();
Iterator iterator = rows.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
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
编写测试插入的方法testInsert()
打开数据库里的表,学生记录插入成功
编写测试插入的方法testDeleteById()
运行结果
编写测试方法testDeleteByClass()
运行结果
编写测试方法testFindByName()
运行结果
如何判断是否找到记录,可以修改代码后再运行
运行结果
创建测试方法testFindRowsBySex()
运行结果
创建测试方法deleteByDepartment(),运行结果为:
创建测试方法testUpdate(),运行结果为:
创建测试方法testFindById(),运行结果为:
创建测试方法testFindByClass(),运行结果为:
创建测试方法testFindByDepartment(),运行结果为:
创建测试方法testFindAll(),运行结果为:
创建测试方法testFindRowsByClass(),运行结果为:
创建测试方法testFindRowsByDepartment(),运行结果为:
创建测试方法testFindRowsBySex(),运行结果为:
在net.hw.student.test包里创建测试类TestUserDaoImpl
package net.xmh.student.bean.test;
import net.xmh.student.bean.User;
import net.xmh.student.bean.dao.UserDaoImpl;
import net.xmh.student.bean.dao.impl.UserDao;
import org.junit.Test;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
/**
功能:测试用户数据访问接口实现类
作者:ygf
日期:2019年6月19日
*/
public class TestUserDaoImpl {
@Test
public void testFindById(){
UserDao dao = new UserDaoImpl();
User user = dao.findById(1);
System.out.println(user);
}
@Test
public void testInsert() {
User user = new User();
user.setUsernname(“王小小”);
user.setId(9);
user.setTelephone(“18086902766”);
user.setRegisterTime(new Timestamp(new Date().getTime()));
user.setPassword(“555”);
UserDao dao = new UserDaoImpl();
int count = dao.insert(user);
if (count > 0){
System.out.println("恭喜,学生记录插入成功!");
}else {
System.out.println("遗憾,学生记录插入失败!");
}
}
@Test
public void testUpdate(){
UserDao dao = new UserDaoImpl();
User user = dao.findById(9);
user.setUsernname(“哒哒”);
int count = dao.update(user);
if (count > 0){
System.out.println(“更新成功!”);
}else{
System.out.println(“更新失败!”);
}
}
@Test
public void testFindAll(){
UserDao dao = new UserDaoImpl();
String all;
List students = dao.findAll();
for (User student : students){
System.out.println(student);
}
}
@Test
public void testDeleteById(){
UserDao dao = new UserDaoImpl();
User user = dao.findById(9);
System.out.println(user);
}
@Test
public void testLogin(){
UserDao dao = new UserDaoImpl();
String username,password;
username = “howard”;
password =“903213”;
User user = dao.login(username,password);
if (user != null){
System.out.println(“恭喜,用户名与密码正确,登录成功!”);
}else {
System.out.println(“遗憾,用户名与密码错误,登录失败!”);
}
}
}
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
编写测试方法testFindById()
运行结果如下:
编写测试方法testLogin(),运行结果如下:
修改一下用户名与密码,再次运行,结果如下:
编写测试方法testInsert(),运行结果如下:
编写测试方法testUpdate(),运行结果如下:
编写测试方法testFindAll(),运行结果如下:
编写测试方法testDeleteById(),运行结果如下: