-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueryRunnerTest.java
More file actions
105 lines (80 loc) · 3.26 KB
/
Copy pathQueryRunnerTest.java
File metadata and controls
105 lines (80 loc) · 3.26 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
package apacheDBUtils;
import jdbc.bean.Customer;
import jdbc.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;
import java.sql.Connection;
import java.sql.Date;
import java.util.List;
/* 封装数据库的增删改查操作*/
public class QueryRunnerTest {
@Test
public void getMaxBirthTest() throws Exception {
Connection connection = null;
try {
QueryRunner queryRunner = new QueryRunner();
connection = JDBCUtils.getConnection3();
String sql = "select max(birth) from customers";
ScalarHandler scalarHandler = new ScalarHandler();
Date date = (Date) queryRunner.query(connection, sql, scalarHandler);
// get the youngest birth day
System.out.println(date);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(connection, null);
}
}
@Test
public void getCountTest() throws Exception {
Connection connection = null;
try {
QueryRunner queryRunner = new QueryRunner();
connection = JDBCUtils.getConnection3();
String sql = "select count(*) from customers";
ScalarHandler scalarHandler = new ScalarHandler();
Object count = queryRunner.query(connection, sql, scalarHandler);
System.out.println(count);
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(connection, null);
}
}
@Test
public void queryTest() throws Exception {
Connection connection = JDBCUtils.getConnection3();
QueryRunner queryRunner = new QueryRunner();
String sql = "select id, name, email, birth from customers where id = ?";
BeanHandler<Customer> handler = new BeanHandler<>(Customer.class);
Customer customer = queryRunner.query(connection, sql, handler, 2);
System.out.println(customer);
}
@Test
public void queryTest2() throws Exception {
Connection connection = JDBCUtils.getConnection3();
QueryRunner queryRunner = new QueryRunner();
String sql = "select id, name, email, birth from customers where id > ?";
BeanListHandler<Customer> handler = new BeanListHandler<>(Customer.class);
List<Customer> list = queryRunner.query(connection, sql, handler, 10);
list.forEach(System.out::println);
}
@Test
public void insertTest() throws Exception {
Connection connection = null;
try {
QueryRunner queryRunner = new QueryRunner();
connection = JDBCUtils.getConnection3();
String sql = "insert into customers (name, email, birth) values(?, ? ,?)";
int dataCount = queryRunner.update(connection, sql, "WuKong", "monkey1@yahoo.com", "1900-01-01");
System.out.println("添加了 " + dataCount + " 条记录");
} catch (Exception e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(connection, null);
}
}
}