Repository navigation
Expand file tree
/
Copy pathDBUtilTest.java
More file actions
143 lines (128 loc) · 4.88 KB
/
Copy pathDBUtilTest.java
File metadata and controls
143 lines (128 loc) · 4.88 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
package com.mashibing.apacheDBUtil;
import com.mashibing.entity.Emp;
import com.mashibing.util.MySQLDBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.*;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class DBUtilTest {
public static Connection connection ;
public static void testQuery() throws SQLException {
connection = MySQLDBUtil.getConnection();
String sql ="select * from emp where empno=?";
QueryRunner runner = new QueryRunner();
Emp query = runner.query(connection, sql, new BeanHandler<Emp>(Emp.class), 7369);
System.out.println(query);
connection.close();
}
public static void testList() throws SQLException {
connection = MySQLDBUtil.getConnection();
String sql ="select * from emp";
QueryRunner runner = new QueryRunner();
List<Emp> query = runner.query(connection, sql, new BeanListHandler<Emp>(Emp.class));
for (Emp emp : query) {
System.out.println(emp);
}
connection.close();
}
public static void testArray() throws SQLException {
connection = MySQLDBUtil.getConnection();
String sql ="select * from emp";
QueryRunner runner = new QueryRunner();
Object[] object = runner.query(connection, sql, new ArrayHandler());
for (Object o : object) {
System.out.println(o);
}
connection.close();
}
public static void testArrayList() throws SQLException {
connection = MySQLDBUtil.getConnection();
String sql ="select * from emp";
QueryRunner runner = new QueryRunner();
List<Object[]> query = runner.query(connection, sql, new ArrayListHandler());
for (Object[] objects : query) {
System.out.println(objects[0]+"--"+objects[1]);
}
connection.close();
}
public static void testMap() throws SQLException {
connection = MySQLDBUtil.getConnection();
String sql ="select * from emp";
QueryRunner runner = new QueryRunner();
Map<String, Object> query = runner.query(connection, sql, new MapHandler());
Set<Map.Entry<String, Object>> entries = query.entrySet();
for (Map.Entry<String, Object> entry : entries) {
System.out.println(entry.getKey()+"---"+entry.getValue());
}
connection.close();
}
public static void testSaclarHandler() throws SQLException {
connection = MySQLDBUtil.getConnection();
String sql ="select count(*) from emp";
QueryRunner runner = new QueryRunner();
Object query = runner.query(connection, sql, new ScalarHandler<>());
System.out.println(query);
connection.close();
}
/**
* 自定义handler对象
* @throws SQLException
*/
public static void testMyHandler() throws SQLException {
connection = MySQLDBUtil.getConnection();
String sql ="select * from emp where empno = ?";
QueryRunner runner = new QueryRunner();
Emp emp = runner.query(connection, sql, new ResultSetHandler<Emp>() {
@Override
public Emp handle(ResultSet resultSet) throws SQLException {
if(resultSet.next()){
Emp e = new Emp();
e.setEmpno(resultSet.getInt("empno"));
e.setEname(resultSet.getString("ename"));
return e;
}
return null;
}
},7369);
System.out.println(emp);
connection.close();
}
public static void insert() throws SQLException {
String sql = "insert into emp(empno,ename) values(?,?)";
connection = MySQLDBUtil.getConnection();
QueryRunner queryRunner = new QueryRunner();
queryRunner.update(connection,sql,1234,"msb");
connection.close();
}
public static void update() throws SQLException {
String sql = "update emp set ename=? where empno = ?";
connection = MySQLDBUtil.getConnection();
QueryRunner queryRunner = new QueryRunner();
queryRunner.update(connection,sql,"mashibing",1234);
connection.close();
}
public static void delete() throws SQLException {
String sql = "delete from emp where empno=?";
connection = MySQLDBUtil.getConnection();
QueryRunner queryRunner = new QueryRunner();
queryRunner.update(connection,sql,1234);
connection.close();
}
public static void main(String[] args) throws SQLException {
// testQuery();
// testList();
// testArray();
// testArrayList();
//testMap();
//testSaclarHandler();
// testMyHandler();
// insert();
// update();
delete();
}
}