DBUtils
概述
DBUtils是java编程中的数据库操作实用工具,小巧简单实用。
DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。
Dbutils三个核心功能介绍
QueryRunner中提供对sql语句操作的API.
ResultSetHandler接口,用于定义select操作后,怎样封装结果集.
DbUtils类,它就是一个工具类,定义了关闭资源与事务处理的方法
QueryRunner核心类
update(Connection conn, String sql, Object... params) ,用来完成表数据的增加、删除、更新操作
query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params) ,用来完成表数据的查询操作
QueryRunner实现添加、更新、删除操作
update(Connection conn, String sql, Object... params) ,用来完成表数据的增加、删除、更新操作
QueryRunner实现查询操作
query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params),用来完成表数据的查询操作
1.1.1 ResultSetHandler结果集处理类
ArrayHandler |
将结果集中的第一条记录封装到一个Object[]数组中,数组中的每一个元素就是这条记录中的每一个字段的值 |
ArrayListHandler |
将结果集中的每一条记录都封装到一个Object[]数组中,将这些数组在封装到List集合中。 |
BeanHandler |
将结果集中第一条记录封装到一个指定的javaBean中。 |
BeanListHandler |
将结果集中每一条记录封装到指定的javaBean中,将这些javaBean在封装到List集合中 |
ColumnListHandler |
将结果集中指定的列的字段值,封装到一个List集合中 |
ScalarHandler |
它是用于单数据。例如select count(*) from 表操作。 |
MapHandler |
将结果集第一行封装到Map集合中,Key 列名, Value 该列数据 |
MapListHandler |
将结果集每一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合 |
JavaBean
- 需要实现接口:java.io.Serializable
- 提供私有字段:private 类型 字段名;
- 提供getter/setter方法:
- 提供无参构造
代码实现:
1 public void addProduct() throws SQLException{ 2 Connection conn=JDBCUtils.getConn(); 3 QueryRunner qr=new QueryRunner(); 4 String sql="insert into product(pid,pname) values(?,?)"; 5 Object[] obj={"1234567","iphoneXS"}; 6 qr.update(conn, sql,obj); 7 DbUtils.closeQuietly(conn); 8 } 9 public void deleteProduct() throws SQLException{ 10 Connection conn=JDBCUtils.getConn(); 11 QueryRunner qr=new QueryRunner(); 12 String sql="delete from product where pname=? "; 13 Object[] obj={"qwdqw"}; 14 qr.update(conn,sql, obj); 15 DbUtils.closeQuietly(conn); 16 } 17 public void editProduct() throws SQLException{ 18 Connection conn=JDBCUtils.getConn(); 19 QueryRunner qr=new QueryRunner(); 20 String sql="update product set pname=? where pid=?"; 21 Object[] obj={"vivoX10","1234567"}; 22 qr.update(conn, sql,obj); 23 DbUtils.closeQuietly(conn); 24 } 25 //ArrayHandler 26 //将结果集中的第一行数据封装到Object[]中
27 public void select1() throws SQLException{ 28 Connection conn=JDBCUtils.getConn(); 29 QueryRunner qr=new QueryRunner(); 30 String sql="select * from product"; 31 Object[] obj=qr.query(conn,sql, new ArrayHandler()); 32 for(Object o:obj){ 33 System.out.println(o); 34 } 35 } 36 //ArrayListHandler 37 //将结果集中的每一行都封装到Object[]中,然后将每一个Object数组封装到一个List集合中
38 public void select2() throws SQLException{ 39 Connection conn=JDBCUtils.getConn(); 40 QueryRunner qr=new QueryRunner(); 41 String sql="select * from product"; 42 List<Object[]> list=qr.query(conn,sql, new ArrayListHandler()); 43 for(Object[] obj:list){ 44 for(Object o:obj){ 45 System.out.println(o+"\t"); 46 } 47 System.out.println(); 48 } 49 } 50 //BeanHandler 51 //将结果集中的第一条记录封装到指定的JavaBean中
52 public void select3() throws SQLException{ 53 QueryRunner qr=new QueryRunner(); 54 Connection conn=JDBCUtils.getConn(); 55 String sql="select * from product"; 56 Product product=qr.query(conn,sql, new BeanHandler<Product>(Product.class)); 57 System.out.println(product); 58 DbUtils.closeQuietly(conn); 59 } 60 //BeanListHandler 61 //将结果集中的每一条记录封装到指定的JavaBean中再将每一个JavaBean封装到List集合中
62 public void select4() throws SQLException{ 63 QueryRunner qr=new QueryRunner(); 64 Connection conn=JDBCUtils.getConn(); 65 String sql="select * from product"; 66 List<Product> list=qr.query(conn,sql, new BeanListHandler<Product>(Product.class)); 67 for(Product p:list){ 68 System.out.println(p); 69 } 70 DbUtils.closeQuietly(conn); 71 } 72 //ColumnListHandler 73 //将结果集中的指定列封装到List集合中
74 public void select5() throws SQLException{ 75 QueryRunner qr=new QueryRunner(); 76 Connection conn=JDBCUtils.getConn(); 77 String sql="select * from product"; 78 List<String> list=qr.query(conn,sql, new ColumnListHandler<String>("pname")); 79 for(String s:list){ 80 System.out.println(s); 81 } 82 DbUtils.closeQuietly(conn); 83 } 84 //ScalarHandler
85 public void select6() throws SQLException{ 86 QueryRunner qr=new QueryRunner(); 87 Connection conn=JDBCUtils.getConn(); 88 String sql="select count(*) from product"; 89 Long count=qr.query(conn,sql, new ScalarHandler<Long>()); 90 System.out.println(count); 91 DbUtils.closeQuietly(conn); 92 } 93
94 public void select7() throws SQLException{ 95 QueryRunner qr=new QueryRunner(); 96 Connection conn=JDBCUtils.getConn(); 97 String sql="select * from product"; 98 Map<String,Object> map=qr.query(conn, sql, new MapHandler()); 99 Set<Map.Entry<String,Object>> set=map.entrySet(); 100 for(Map.Entry<String,Object> entry:set){ 101 System.out.println(entry.getKey()+"..."+entry.getValue()); 102 } 103 DbUtils.closeQuietly(conn); 104 } 105 public void select8() throws SQLException{ 106 QueryRunner qr=new QueryRunner(DBUtils.getDataSource()); 107 //Connection conn=JDBCUtils.getConn();
108 String sql="select * from product"; 109 List<Map<String,Object>> list=qr.query(sql, new MapListHandler()); 110 for(Map<String,Object> map:list){ 111 Set<Map.Entry<String,Object>> set=map.entrySet(); 112 for(Map.Entry<String,Object> entry:set){ 113 System.out.println(entry.getKey()+"..."+entry.getValue()); 114 } 115 } 116
117 //DbUtils.closeQuietly(conn);
118 } 119 }
连接池
连接池概述
概念
用池来管理Connection,这样可以重复使用Connection。有了池,所以我们就不用自己来创建Connection,而是通过池来获取Connection对象。当使用完Connection后,调用Connection的close()方法也不会真的关闭Connection,而是把Connection“归还”给池。池就可以再利用这个Connection对象了
DBCP连接池
1 public class JDBCUtils { 2
3
4 public static final String DRIVER = "com.mysql.jdbc.Driver"; 5
6
7 public static final String URL = "jdbc:mysql://localhost:3306/daydb"; 8
9
10 public static final String USERNAME = "root"; 11
12
13 public static final String PASSWORD = "root"; 14
15
16 /*
17
18
19 * 创建连接池BasicDataSource 20
21
22 */
23
24
25 public static BasicDataSource dataSource = new BasicDataSource(); 26
27
28 //静态代码块
29
30
31 static { 32
33
34 //对连接池对象 进行基本的配置
35
36
37 dataSource.setDriverClassName(DRIVER); // 这是要连接的数据库的驱动
38
39
40 dataSource.setUrl(URL); //指定要连接的数据库地址
41
42
43 dataSource.setUsername(USERNAME); //指定要连接数据的用户名
44
45
46 dataSource.setPassword(PASSWORD); //指定要连接数据的密码
47
48
49 } 50
51
52 /*
53
54
55 * 返回连接池对象 56
57
58 */
59
60
61 public static DataSource getDataSource(){ 62
63
64 return dataSource; 65
66
67 } 68
69
70 }
工具类的使用
1 测试类 2
3
4 /*
5
6
7 * 演示使用DBUtils工具 完成数据库表的增加操作 8
9
10 */
11
12
13 public class Demo { 14
15
16 // 插入功能
17
18
19 @Test 20
21
22 public void insert(){ 23
24
25 try { 26
27
28 //获取一个用来执行SQL语句的对象 QueryRunner
29
30
31 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 32
33
34 String sql = "INSERT INTO zhangwu(name,money,parent) VALUES(?,?,?)"; 35
36
37 Object[] params = {"股票收入", 5500, "收入"}; 38
39
40 int line = qr.update(sql,params); 41
42
43 //结果集处理
44
45
46 System.out.println("line = " + line); 47
48
49
50
51
52 } catch (SQLException e) { 53
54
55 throw new RuntimeException(e); 56
57
58 } 59
60
61 } 62
63
64
65
66
67 //删除功能
68
69
70 @Test 71
72
73 public void delete(){ 74
75
76 try { 77
78
79 //创建一个QueryRunner对象,用来完成SQL语句的执行
80
81
82 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 83
84
85 //执行SQL语句
86
87
88 String sql = "DELETE FROM zhangwu WHERE name = ?"; 89
90
91 Object[] params = {"股票收入"}; 92
93
94 int line = qr.update(sql, params); 95
96
97 //结果集的处理
98
99
100 System.out.println("line="+line); 101
102
103
104
105
106 } catch (SQLException e) { 107
108
109 throw new RuntimeException(e); 110
111
112 } 113
114
115 } 116
117
118
119
120
121 //更新功能
122
123
124 @Test 125
126
127 public void update(){ 128
129
130 try { 131
132
133 //创建一个QueryRunner对象,用来完成SQL语句的执行
134
135
136 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 137
138
139 //执行SQL语句
140
141
142 String sql = "UPDATE zhangwu SET money = money+1000 WHERE name=?"; 143
144
145 Object[] params = {"股票收入"}; 146
147
148 int line = qr.update(sql, params); 149
150
151 //结果集的处理
152
153
154 System.out.println("line="+line); 155
156
157
158
159
160 } catch (SQLException e) { 161
162
163 throw new RuntimeException(e); 164
165
166 } 167
168
169 } 170
171
172
173
174
175 //查询功能,将结果集中第一条记录封装到一个指定的javaBean中。
176
177
178 @Test 179
180
181 public void search(){ 182
183
184 try{ 185
186
187 //获取QueryRunner
188
189
190 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 191
192
193 //执行SQL语句
194
195
196 String sql = "SELECT * FROM zhangwu"; 197
198
199 Object[] params = {}; 200
201
202 Product p = qr.query(sql, new BeanHandler<Product>(Product.class), params); 203
204
205 //结果集处理
206
207
208 System.out.println(p); 209
210
211
212
213
214 } catch(SQLException e){ 215
216
217 throw new RuntimeException(e); 218
219
220 } 221
222
223 } 224
225
226 }
常见配置项
分类 |
属性 |
描述 |
必须项 |
driverClassName |
数据库驱动名称 |
url |
数据库的地址 |
|
username |
用户名 |
|
password |
密码 |
|
基本项(扩展) |
maxActive |
最大连接数量 |
minIdle |
最小空闲连接 |
|
maxIdle |
最大空闲连接 |
|
initialSize |
初始化连接 |
总结
DBUtils工具
作用:简化JDBC的操作
常用类与方法
QueryRunner 用来执行SQL语句对象
update(Connection conn, String sql, Object… params) 插入表记录、更新表记录、删除表记录
query(Connection conn, String sql, ResultSetHandler handler, Object… params) 查询表记录
ResultSetHandler 处理结果集的对象
ArrayHandler |
将结果集中的第一条记录封装到一个Object[]数组中,数组中的每一个元素就是这条记录中的每一个字段的值 |
ArrayListHandler |
将结果集中的每一条记录都封装到一个Object[]数组中,将这些数组在封装到List集合中。 |
BeanHandler |
将结果集中第一条记录封装到一个指定的javaBean中。 |
BeanListHandler |
将结果集中每一条记录封装到指定的javaBean中,将这些javaBean在封装到List集合中 |
ColumnListHandler |
将结果集中指定的列的字段值,封装到一个List集合中 |
ScalarHandler |
它是用于单数据。例如select count(*) from 表操作。 |
MapHandler |
将结果集第一行封装到Map集合中,Key 列名, Value 该列数据 |
MapListHandler |
将结果集第一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合 |
DBCP连接池
作用:自身维护了多个Connection连接对象维护
BasicDataSource类 是 DataSource接口的实现类
DataSource接口,它是java与每种数据库连接池 连接的规范标准
DBCP连接池常见的配置
必须项 |
driverClassName |
数据库驱动名称 |
url |
数据库的地址 |
|
username |
用户名 |
|
password |
密码 |
|
基本项 |
maxActive |
最大连接数量 |
initialSize |
连接池中初始化多少个Connection连接对象 |
|
扩展项 |
maxWait |
超时等待时间以毫秒为单位 1000等于1秒 |