数据库连接池以及C3P0和DBCP

数据库连接池

C3P0

DBCP

DBUtil

有关数据库连接池:

1.为什么要使用数据库连接池?

资源重用性、自己创建消耗资源

便于管理数据库的连接

2.两种实现方式:C3P0数据库连接池 和 DBCP数据库连接池

如上的两种方式都是属于第三方的jar包,故而在使用之前都需要导包,当然连接第三方数据库也需要导入第三方数据库的相关驱动。

C3P0:

一般解压C3P0的压缩包,在其lib包下的c3p0-0.9.1.2.jar,就是需要的jar包。

ComboPoolDataSource 类是第三方提供的DataSource接口的实现类。

简单的获取数据库连接方式:

ComboPooledDataSource cpds = new ComboPooledDataSource();

cpds.setDriverClass("com.mysql.jdbc.Driver");

cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");

cpds.setUser("root");

cpds.setPassword("wang");

Connection conn = cpds.getConnection();

另外还可以使用配置文件的方式获取链接:

DataSource cpds = new ComboPooledDataSource("helloc3p0");

Connection conn = cpds.getConnection();

其中配置文件c3p0-config.xml位于src目录下(非project目录下)

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>


<named-config name="helloc3p0">

<!-- 获取连接的4个基本信息 -->

<property name="driverClass">com.mysql.jdbc.Driver</property>

<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>

<property name="user">root</property>

<property name="password">abc123</property>

<!-- 当连接池中的连接数不够时,c3p0数据源一次性向数据库服务器申请的连接数 -->

<property name="acquireIncrement">5</property>

<!-- 初始化时,数据库连接池中的连接数 -->

<property name="initialPoolSize">10</property>

<!-- 连接池中维护的最少的连接数 -->

<property name="minPoolSize">5</property>

<!-- 连接池中维护的最大的连接数  -->

<property name="maxPoolSize">100</property>

<!-- 连接池中最多可以维护的Statement的个数 -->

<property name="maxStatements">100</property>

<!-- 每一个连接最多可以维护的Statement的个数 -->

<property name="maxStatementsPerConnection">2</property>


</named-config>

</c3p0-config>

DBCP 数据库连接池:

使用DBCP数据库连接池,需要导入两个jar包,

commons-dbcp-1.4下的commons-dbcp-1.4.jar 和

commons-pool-1.5.5包下的 commons-pool-1.5.5.jar

BasicDataSource类也同样是第三方提供的实现了DataSource接口的类。

简单获取数据库连接的方式:

BasicDataSource source = new BasicDataSource();

source.setDriverClassName("com.mysql.jdbc.Driver");

source.setUrl("jdbc:mysql://localhost:3306/test");

source.setUsername("root");

source.setPassword("abc123");

source.setMaxActive(50);

source.setInitialSize(10);

Connection conn = source.getConnection();

使用配置文件获取数据库连接的方式:

Properties pros = new Properties();

pros.load(this.getClass().getClassLoader()

.getResourceAsStream("dbcp.properties"));

DataSource source = BasicDataSourceFactory.createDataSource(pros);

Connection conn = source.getConnection();

DBUtils

第三方的操作数据库的jar包,其中封装了对数据库的操作。

常用类QueryRunner类。

public int update(Connection con,String sql,Object.. args)返回此操作影响的记录数目。

public < T> T query(Connection con,String sql,ResultSetHandler rsh ,Object …args) 其返回sql语句的查询结果,结果保存在实现了ResultSetHandler接口的具体类中。该对象用来将结果集转换成一个java对象。

如果只返回一个记录,可以将一条记录封装到一个对应类的对象里面,那么就可以使用BeanHandler对象来接受。

如果返回多条记录,可以使用BeanListHandler对象来接受。

如果返回一条记录,那么这条记录与表头上下同样构成了一个Map,可以使用MapHandler来接受。

如果返回多条记录,可以使用MapListHandler来接受。

如果返回记录中的特殊值,可以使用ScalarHandler来接受。

public ScalarHandler(String columnName);

public ScalarHandler();

public BeanHandler(Class clazz);

返回一条记录

Connection conn = DBCP.getConnection();

QueryRunner qr = new QueryRunner();

String sql = “select id,name,email,birth from customers where id = ?”;

ResultSetHandler< Customer> rsh= new BeanHandler< Customer>(Customer.class);

Customer cus = qr.query(conn, sql, rsh, 1);

返回多条结果

Connection conn = DBCP.getConnection();

QueryRunner qr = new QueryRunner();

String sql = "select id ,name,email ,birth from customers where id < ?";

ResultSetHandler< List< Customer>> rsh = new BeanListHandler< Customer>(Customer.class);

List< Customer> list = qr.query(conn, sql, rsh, 5);

使用ScalarHandler来接受返回特殊结果(首行首列的数值)

Connection conn = DBCP.getConnection();

QueryRunner qr = new QueryRunner();

String sql = “select count(*) from customers”;

ResultSetHandler rsh = new ScalarHandler();

Object resul = qr.query(conn, sql, rsh);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会回答你的问题,以下是常用数据库连接池 DBCPc3p0、Druid 的配置说明: 1. DBCP (Apache Commons DBCP) DBCP 是 Apache Commons 项目中的一个子项目,它是一个纯 Java 实现的数据库连接池DBCP 的使用非常简单,只需要引入相关的 jar 包即可。以下是 DBCP 的配置说明: ``` # 最大连接数 maxActive=100 # 最大空闲连接数 maxIdle=30 # 最小空闲连接数 minIdle=10 # 获取连接时的最大等待时间,单位为毫秒 maxWait=10000 # 是否开启自动回收空闲连接的线程 removeAbandoned=true # 回收超时时间,单位为秒 removeAbandonedTimeout=180 # 是否开启缓存 PreparedStatement,提高性能 poolPreparedStatements=true # 缓存 PreparedStatement 的最大数量 maxOpenPreparedStatements=100 ``` 2. c3p0 c3p0 是一个开源的 JDBC 数据库连接池,它实现了数据源和 JNDI 绑定,支持 JDBC3 的 Connection 和 Statement 缓存以及 JDBC4 的自动化管理。以下是 c3p0 的配置说明: ``` # 最大连接数 c3p0.maxPoolSize=100 # 最小连接数 c3p0.minPoolSize=10 # 初始化连接数 c3p0.initialPoolSize=10 # 获取连接时的最大等待时间,单位为毫秒 c3p0.checkoutTimeout=10000 # 是否自动回收超时连接 c3p0.autoCommitOnClose=true # 是否开启自动回收空闲连接的线程 c3p0.idleConnectionTestPeriod=60 # 回收超时时间,单位为秒 c3p0.maxIdleTime=1800 # 是否开启缓存 PreparedStatement,提高性能 c3p0.cachePreparedStatements=true # 缓存 PreparedStatement 的最大数量 c3p0.maxStatements=100 ``` 3. Druid Druid 是阿里巴巴开源的一个高性能、可扩展、功能强大的数据库连接池。它主要提供了以下功能:监控统计、防御 SQL 注入、批量处理、数据源加密、日志记录等。以下是 Druid 的配置说明: ``` # 最大连接数 druid.maxActive=100 # 最大空闲连接数 druid.maxIdle=30 # 最小空闲连接数 druid.minIdle=10 # 获取连接时的最大等待时间,单位为毫秒 druid.maxWait=10000 # 是否开启自动回收空闲连接的线程 druid.removeAbandoned=true # 回收超时时间,单位为秒 druid.removeAbandonedTimeout=180 # 是否开启缓存 PreparedStatement,提高性能 druid.poolPreparedStatements=true # 缓存 PreparedStatement 的最大数量 druid.maxOpenPreparedStatements=100 # 是否开启 SQL 执行监控 druid.stat=true # 是否开启防御 SQL 注入功能 druid.filters=stat,wall,log4j ``` 以上就是常用数据库连接池 DBCPc3p0、Druid 的配置说明。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值