java中DButil谁调用的_java--DBUtils和连接池

DBUtils是Apache Commons库中的一个简化JDBC操作的工具,它提供了QueryRunner类进行数据库的增删改查操作。QueryRunner的update方法用于更新,如添加、删除和修改数据,而query方法用于查询,配合ResultSetHandler接口处理查询结果。此外,DBUtils还提供了DbUtils类用于关闭资源和事务管理。常用的ResultSetHandler包括ArrayHandler、ArrayListHandler、BeanHandler、BeanListHandler、ColumnListHandler、ScalarHandler、MapHandler和MapListHandler等。
摘要由CSDN通过智能技术生成

DBUtils

一、DBUtils的使用

DBUtils就是JDBC的简化开发工具包。需要项目导入commons-dbutils-1.6.jar才能够正常使用DBUtils工具。如果只使用JDBC进行开发,我们会发现冗余代码过多,为了简化JDBC开发,本案例我们讲采用apache commons组件一个成员:DBUtils。

86566e9aeb1fc84cb3fcd28314538076.png

二、DBUtils概述

DBUtils是java编程中的数据库操作实用工具,小巧简单实用。DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。

Dbutils三个核心功能介绍:

1.QueryRunner中提供对sql语句操作的API.

2.ResultSetHandler接口,用于定义select操作后,怎样封装结果集.

3. DbUtils类,它就是一个工具类,定义了关闭资源与事务处理的方法

三、QueryRunner核心类

1.update(Connection conn, String sql, Object... params) ,用来完成表数据的增加、删除、更新操作

2.query(Connection conn, String sql, ResultSetHandler rsh, Object... params) ,用来完成表数据的查询操作

四、QueryRunner实现添加、更新、删除操作

//添加

public void addProduct() throwsSQLException{

Connection conn=JDBCUtils.getConn();

QueryRunner qr=newQueryRunner();

String sql="insert into product(pid,pname) values(?,?)";

Object[] obj={"1234567","iphoneXS"};

qr.update(conn, sql,obj);

DbUtils.closeQuietly(conn);

}//删除

public void deleteProduct() throwsSQLException{

Connection conn=JDBCUtils.getConn();

QueryRunner qr=newQueryRunner();

String sql="delete from product where pname=? ";

Object[] obj={"qwdqw"};

qr.update(conn,sql, obj);

DbUtils.closeQuietly(conn);

}//更新

public void editProduct() throwsSQLException{

Connection conn=JDBCUtils.getConn();

QueryRunner qr=newQueryRunner();

String sql="update product set pname=? where pid=?";

Object[] obj={"vivoX10","1234567"};

qr.update(conn, sql,obj);

DbUtils.closeQuietly(conn);

}

五、QueryRunner实现查询操作

ResultSetHandler结果集处理类

ArrayHandler

将结果集中的第一条记录封装到一个Object[]数组中,数组中的每一个元素就是这条记录中的每一个字段的值

ArrayListHandler

将结果集中的每一条记录都封装到一个Object[]数组中,将这些数组在封装到List集合中。

BeanHandler

将结果集中第一条记录封装到一个指定的javaBean中。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    //ArrayHandler2 //将结果集中的第一行数据封装到Object[]中

3 public void select1() throwsSQLException{4 Connection conn=JDBCUtils.getConn();5 QueryRunner qr=newQueryRunner();6 String sql="select * from product";7 Object[] obj=qr.query(conn,sql, newArrayHandler());8 for(Object o:obj){9 System.out.println(o);10 }11 DbUtils.closeQuietly(conn);12 }13 //ArrayListHandler14 //将结果集中的每一行都封装到Object[]中,然后将每一个Object数组封装到一个List集合中

15 public void select2() throwsSQLException{16 Connection conn=JDBCUtils.getConn();17 QueryRunner qr=newQueryRunner();18 String sql="select * from product";19 List list=qr.query(conn,sql, newArrayListHandler());20 for(Object[] obj:list){21 for(Object o:obj){22 System.out.println(o+"\t");23 }24 System.out.println();25 }26 DbUtils.closeQuietly(conn);27 }28 //BeanHandler29 //前提:JavaBean必须有空参构造和set方法30 //将结果集中的第一条记录封装到指定的JavaBean中

31 public void select3() throwsSQLException{32 QueryRunner qr=newQueryRunner();33 Connection conn=JDBCUtils.getConn();34 String sql="select * from product";35 Product product=qr.query(conn, sql,new BeanHandler(Product.class));36 System.out.println(product);37 DbUtils.closeQuietly(conn);38 }39 //BeanListHandler40 //将结果集中每一条记录封装到指定的javaBean中,将这些javaBean在封装到List集合中

41 public void select4() throwsSQLException{42 QueryRunner qr=newQueryRunner();43 Connection conn=JDBCUtils.getConn();44 String sql="select * from product";45 List list=qr.query(conn, sql,new BeanListHandler(Product.class));46 for(Product p:list){47 System.out.println(p);48 }49 DbUtils.closeQuietly(conn);50 }51 //ColumnListHandler52 //将结果集中指定的列的字段值,封装到一个List集合中

53 public void select5()throwsSQLException{54 QueryRunner qr=newQueryRunner();55 Connection conn=JDBCUtils.getConn();56 String sql="select * from product";57 List list=qr.query(conn, sql,new ColumnListHandler("pname"));58 for(String s:list){59 System.out.println(s);60 }61 DbUtils.closeQuietly(conn);62 }63 //ScalarHandler64 //它是用于单数据。例如select count(*) from 表操作。

65 public void select6()throwsSQLException{66 QueryRunner qr=newQueryRunner();67 Connection conn=JDBCUtils.getConn();68 String sql="select count(*) from product";69 Long count=qr.query(conn, sql,new ScalarHandler());70 System.out.println(count);71 DbUtils.closeQuietly(conn);72 }73 //MapHandler74 //将结果集第一行封装到Map集合中,Key 列名, Value 该列数据

75 public void select7()throwsSQLException{76 QueryRunner qr=newQueryRunner();77 Connection conn=JDBCUtils.getConn();78 String sql="select count(*) from product";79 Map map=qr.query(conn, sql,newMapHandler());80 Set> set=map.entrySet();81 for(Map.Entryentry: set){82 System.out.println(entry.getKey()+"..."+entry.getValue());83 }84 DbUtils.closeQuietly(conn);85 }86 //MapListHandler87 //将结果集第一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合

88 public void select8()throwsSQLException{89 QueryRunner qr=newQueryRunner(DButils.getDataSource());90 //Connection conn=JDBCUtils.getConn();

91 String sql="select count(*) from product";92 List> list=qr.query(sql,newMapListHandler());93 for(Mapmap:list){94 Set> set=map.entrySet();95 for(Map.Entryentry: set){96 System.out.println(entry.getKey()+"..."+entry.getValue());97 }98 }99 }

连接池

一、连接池概述

用池来管理Connection,这样可以重复使用Connection。有了池,所以我们就不用自己来创建Connection,而是通过池来获取Connection对象。当使用完Connection后,调用Connection的close()方法也不会真的关闭Connection,而是把Connection“归还”给池。池就可以再利用这个Connection对象了。

aad5b37ba7bb1a091026cf6e3f31e8c0.png

规范:

Java为数据库连接池提供了公共的接口:javax.sql.DataSource,各个厂商需要让自己的连接池实现这个接口。这样应用程序可以方便的切换不同厂商的连接池!

常见的连接池:DBCP、C3P0。

二、DBCP连接池

1.使用:导包

275727a06d370fd4ba3f76594f0a6bde.png

2.编写工具类

1 packagecom.oracle.tools;2

3 importjava.sql.Connection;4 importjava.sql.SQLException;5

6 importjavax.sql.DataSource;7

8 importorg.apache.commons.dbcp.BasicDataSource;9

10

11 public classDButils {12 public static final String DRIVER = "com.mysql.jdbc.Driver";13 public static final String URL = "jdbc:mysql://localhost:3306/bank";14 public static final String USERNAME = "root";15 public static final String PASSWORD = "123456";16 /*

17 * 创建连接池BasicDataSource18 */

19 public static BasicDataSource dataSource = newBasicDataSource();20 public static ThreadLocal tl=new ThreadLocal();21 //静态代码块

22 static{23 //对连接池对象 进行基本的配置

24 dataSource.setDriverClassName(DRIVER); //这是要连接的数据库的驱动

25 dataSource.setUrl(URL); //指定要连接的数据库地址

26 dataSource.setUsername(USERNAME); //指定要连接数据的用户名

27 dataSource.setPassword(PASSWORD); //指定要连接数据的密码

28 }29 /*

30 * 返回连接池对象31 */

32 //获取当前线程上的连接

33 public staticConnection getCurrentConnection(){34 Connection conn=tl.get();35 if(conn==null){36 conn=getConn();37 tl.set(conn);38 }39 returnconn;40 }41 //开启事务的方法

42 public static voidstart(){43 try{44 getCurrentConnection().setAutoCommit(false);45 } catch(SQLException e) {46 //TODO Auto-generated catch block

47 e.printStackTrace();48 }49 }50 //提交事务的方法

51 public static voidcommit(){52 try{53 getCurrentConnection().commit();54 } catch(SQLException e) {55 //TODO Auto-generated catch block

56 e.printStackTrace();57 }58 }59 //回滚事务方法

60 public static voidrollback(){61 try{62 getCurrentConnection().rollback();63 } catch(SQLException e) {64 //TODO Auto-generated catch block

65 e.printStackTrace();66 }67 }68 //从连接池里获取一个新连接

69 public staticDataSource getDataSource(){70 returndataSource;71 }72 public staticConnection getConn(){73 Connection conn=null;74 try{75 conn=dataSource.getConnection();76 } catch(SQLException e) {77 //TODO Auto-generated catch block

78 e.printStackTrace();79 }80 returnconn;81 }82 }

3.测试:

1 //MapListHandler2 //将结果集第一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合

3 public void select8()throwsSQLException{4 QueryRunner qr=new QueryRunner(DButils.getDataSource());//有参构造5 //Connection conn=JDBCUtils.getConn();

6 String sql="select count(*) from product";7 List> list=qr.query(sql,newMapListHandler());8 for(Mapmap:list){9 Set> set=map.entrySet();10 for(Map.Entryentry: set){11 System.out.println(entry.getKey()+"..."+entry.getValue());12 }13 }14 }

4.常见配置项(了解)

参考文档:http://commons.apache.org/proper/commons-dbcp/configuration.html

分类

属性

描述

必须项

driverClassName

数据库驱动名称

url

数据库的地址

username

用户名

password

密码

基本项(扩展)

maxActive

最大连接数量

minIdle

最小空闲连接

maxIdle

最大空闲连接

initialSize

连接池中初始化多少个Connection连接对象

扩展项

maxWait

超时等待时间以毫秒为单位 1000等于1秒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值