javaWeb_JDBC_c3p0数据库连接池
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javaWeb_JDBC_c3p0数据库连接池,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2420字,纯文字阅读大概需要4分钟。
内容图文
JDBC_c3p0数据库连接池
1.一个基本的c3p0数据库连接池
/**
* 一个基本的从池c3p0数据库连接池
*/
public static void testC3P0() throws Exception{
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver
cpds.setJdbcUrl( "jdbc:mysql:///test" );
cpds.setUser("root");
cpds.setPassword("123456");
System.out.println(cpds.getConnection());
}
2.通过配置文件来设置c3p0数据库连接池
(1).配置文件
c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="helloc3p0">
<!-- 指定连接数据源的基本属性 -->
<property name="user">root</property>
<property name="password">1230</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///atguigu</property>
<!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
<property name="acquireIncrement">5</property>
<!-- 初始化数据库连接池时连接的数量 -->
<property name="initialPoolSize">5</property>
<!-- 数据库连接池中的最小的数据库连接数 -->
<property name="minPoolSize">5</property>
<!-- 数据库连接池中的最大的数据库连接数 -->
<property name="maxPoolSize">10</property>
<!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
<property name="maxStatements">20</property>
<!-- 每个连接同时可以使用的 Statement 对象的个数 -->
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
(2).测试代码
/**
* 1. 创建 c3p0-config.xml 文件,
* 参考帮助文档中 Appendix B: Configuation Files 的内容
* 2. 创建 ComboPooledDataSource 实例;
* DataSource dataSource =
* new ComboPooledDataSource("helloc3p0");
* 3. 从 DataSource 实例中获取数据库连接.
*/
public void testC3poWithConfigFile() throws Exception{
DataSource dataSource =
new ComboPooledDataSource("helloc3p0");
System.out.println(dataSource.getConnection());
ComboPooledDataSource comboPooledDataSource =
(ComboPooledDataSource) dataSource;
System.out.println(comboPooledDataSource.getMaxStatements());
}
3.数据库连接池应用
(1).创建连接的方法
private static DataSource dataSource = null;
//数据库连接池应只被初始化一次.
static{
dataSource = new ComboPooledDataSource("helloc3p0");
}
public static Connection getConnection() throws Exception {
return dataSource.getConnection();
}
(2).测试代码
public void testJdbcTools() throws Exception{
Connection connection = JDBCTools.getConnection();
System.out.println(connection);
}
原文:https://www.cnblogs.com/nwxayyf/p/10354226.html
内容总结
以上是互联网集市为您收集整理的javaWeb_JDBC_c3p0数据库连接池全部内容,希望文章能够帮你解决javaWeb_JDBC_c3p0数据库连接池所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。