forked from aofeng/JavaTutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDbUtilsDemo.java
More file actions
205 lines (176 loc) · 6.45 KB
/
DbUtilsDemo.java
File metadata and controls
205 lines (176 loc) · 6.45 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
package cn.aofeng.demo.dbutils;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.log4j.Logger;
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;
/**
* Apache DbUtils使用示例。
*
* @author <a href="mailto:aofengblog@163.com">聂勇</a>
*/
public class DbUtilsDemo {
private static Logger _logger = Logger.getLogger(DbUtilsDemo.class);
/**
* 创建JDBC连接池。
*
* @param pros 数据库连接信息,里面包含4个键值对。
* <pre>
* jdbcDriver => JDBC驱动类名称
* url => 数据库JDBC连接地址
* user => 连接数据库的用户名
* password => 连接数据库的密码
* <pre>
* @return 连接池对象
*/
private DataSource createDataSource(Properties pros) {
MysqlDataSource ds = new MysqlDataSource();
ds.setURL(pros.getProperty("url"));
ds.setUser(pros.getProperty("user"));
ds.setPassword(pros.getProperty("password"));
return ds;
}
/**
* 将查询结果集转换成Bean列表返回。
*
* @param ds JDBC连接池
*/
public void queryBeanList(DataSource ds) {
String sql = "select userId, userName, gender, age from student";
QueryRunner run = new QueryRunner(ds);
ResultSetHandler<List<Student>> handler = new BeanListHandler<Student>(Student.class);
List<Student> result = null;
try {
result = run.query(sql, handler);
} catch (SQLException e) {
_logger.error("获取JDBC连接出错或执行SQL出错", e);
}
if (null == result) {
return;
}
for (Student student : result) {
System.out.println(student);
}
}
/**
* 将查询结果转换成Bean返回。
*
* @param ds JDBC连接池
*/
public void queryBean(DataSource ds, int userId) {
String sql = "select userId, userName, gender, age from student where userId=?";
QueryRunner run = new QueryRunner(ds);
ResultSetHandler<Student> handler = new BeanHandler<Student>(Student.class);
Student result = null;
try {
result = run.query(sql, handler, userId);
} catch (SQLException e) {
_logger.error("获取JDBC连接出错或执行SQL出错", e);
}
System.out.println(result);
}
/**
* 将查询结果集转换成键值对列表返回。
*
* @param ds JDBC连接池
*/
public void queryMapList(DataSource ds) {
String sql = "select userId, userName, gender, age from student";
QueryRunner run = new QueryRunner(ds);
ResultSetHandler<List<Map<String, Object>>> handler = new MapListHandler();
List<Map<String, Object>> result = null;
try {
result = run.query(sql, handler);
} catch (SQLException e) {
_logger.error("获取JDBC连接出错或执行SQL出错", e);
}
if (null == result) {
return;
}
for (Map<String, Object> map : result) {
System.out.println(map);
}
}
/**
* 将查询结果集转换成键值对列表返回。
*
* @param ds JDBC连接池
* @param userId 用户编号
*/
public void queryMap(DataSource ds, int userId) {
String sql = "select userId, userName, gender, age from student where userId=?";
QueryRunner run = new QueryRunner(ds);
ResultSetHandler<Map<String, Object>> handler = new MapHandler();
Map<String, Object> result = null;
try {
result = run.query(sql, handler, userId);
} catch (SQLException e) {
_logger.error("获取JDBC连接出错或执行SQL出错", e);
}
System.out.println(result);
}
/**
* 将查询结果集转换成键值对列表返回。
*
* @param ds JDBC连接池
*/
public void queryCustomHandler(DataSource ds) {
String sql = "select userId, userName, gender, age from student";
// 新实现一个ResultSetHandler
ResultSetHandler<List<Student>> handler = new ResultSetHandler<List<Student>>() {
@Override
public List<Student> handle(ResultSet resultset)
throws SQLException {
List<Student> result = new ArrayList<Student>();
while (resultset.next()) {
Student student = new Student();
student.setUserId(resultset.getInt("userId"));
student.setUserName(resultset.getString("userName"));
student.setGender(resultset.getString("gender"));
student.setAge(resultset.getInt("age"));
result.add(student);
}
return result;
}
};
QueryRunner run = new QueryRunner(ds);
List<Student> result = null;
try {
result = run.query(sql, handler);
} catch (SQLException e) {
_logger.error("获取JDBC连接出错或执行SQL出错", e);
}
if (null == result) {
return;
}
for (Student student : result) {
System.out.println(student);
}
}
public static void main(String[] args) {
Properties pros = new Properties();
pros.put("jdbcDriver", "com.mysql.jdbc.Driver");
pros.put("url", "jdbc:mysql://192.168.56.102:19816/test?useUnicode=true&characterEncoding=UTF8");
pros.put("user", "uzone");
pros.put("password", "uzone");
DbUtilsDemo demo = new DbUtilsDemo();
DataSource ds = demo.createDataSource(pros);
demo.queryBeanList(ds);
demo.queryBean(ds, 1);
demo.queryBean(ds, 9);
demo.queryMapList(ds);
demo.queryMap(ds, 3);
demo.queryMap(ds, 9);
demo.queryCustomHandler(ds);
}
}