/*
* c3p0数据库连接池:
* 只被初始化一次
* connection对象进行close时,不是正的关闭,而是将该数据连接归还给数据库连接池
*
* */

四个架包

mysql-connector-java-jar

commons-dbcp-1.4jar

commons-pool-1.5.5jar

c3p0-0.9.1.2.jar(在架包//其他,文件下)导进去

----------------------------------------------------------------------------------------------

//方法一
public void testC3p0() throws Exception{
ComboPooledDataSource cpds=new ComboPooledDataSource();

cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql:///lxn");
cpds.setUser("root");
cpds.setPassword("lxn123");
System.out.println(cpds.getConnection());
}

-----------------------------------------------------------------------------------------------
//方法二:通过配置文件的方法连接数据库

首先在src目录下建立一个XML文件

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

<named-config name="helloc3p0">

<!-- 指定连接数据源的基本属性 -->
<property name="user">root</property>
<property name="password">lxn123</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql:///lxn</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>

连接方法:
public void testC3p0WithConfigFile() throws Exception{
DataSource dataSource=new ComboPooledDataSource("helloc3po");
System.out.println(dataSource.getConnection());

ComboPooledDataSource comboPooledDataSource=(ComboPooledDataSource) dataSource;
System.out.println(comboPooledDataSource.getMaxStatements());
}

------------------------------------------------------------------------------------------------------------
//方法三:在src目录下建立一个XML文件,例上

连接测试方法:
private static DataSource dataSource=null;

static{
dataSource=new ComboPooledDataSource("helloc3p0");
}

public static Connection getConnection() throws Exception{
return dataSource.getConnection();
}

@Test
public void testC3p0L() throws Exception{
Connection connection=getConnection();
System.out.println(connection);
}

c3p0数据库连接池(作用不重复)的更多相关文章

  1. C3P0数据库连接池使用方法

    一.应用程序直接获取数据库连接的缺点 用户每次请求都需要向数据库获得链接,而数据库创建连接通常需要消耗相对较大的资源,创建时间也较长.假设网站一天10万访问量,数据库服务器就需要创建10万次连接,极大 ...

  2. c3p0 数据库连接池相关知识

    c3p0数据库连接池的配置文件放在eclipse的src目录下,代码就可以识别. c3p0的配置文件的内容如下: <!-- Uncomment and set any of the option ...

  3. 【Java EE 学习 16 上】【dbcp数据库连接池】【c3p0数据库连接池】

    一.回顾之前使用的动态代理的方式实现的数据库连接池: 代码: package day16.utils; import java.io.IOException; import java.lang.ref ...

  4. paip.c3p0 数据库连接池 NullPointerException 的解决...

    paip.c3p0 数据库连接池 NullPointerException 的解决... 程序ide里面运行正常..外面bat运行错误.. 作者Attilax  艾龙,  EMAIL:14665198 ...

  5. paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out

    paip.提升稳定性---c3p0数据库连接池不能取到连接An attempt by a client to checkout a Connection has timed out 作者Attilax ...

  6. [原创]java WEB学习笔记80:Hibernate学习之路--- hibernate配置文件:JDBC 连接属性,C3P0 数据库连接池属性等

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  7. springboot 使用c3p0数据库连接池

    springboot 使用c3p0数据库连接池的方法  本文转自:http://www.cnblogs.com/xiaosiyuan/p/6255292.html 使用springboot开发时,默认 ...

  8. c3p0数据库连接池无法连接数据库—错误使用了username关键字

    一.问题描述 上篇博客说到了关于maven无法下载依赖jar包的问题,这篇博客再说一下关于在本个项目中遇到的关于使用C3P0连接池连接数据库的问题,真心很奇葩,在此,也请大家引起注意.首先看我的项目基 ...

  9. c3p0数据库连接池 原创: Java之行 Java之行 5月8日 一、连接池概述 实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程

    c3p0数据库连接池 原创: Java之行 Java之行 5月8日 一.连接池概述 实际开发中“获得连接”或“释放资源”是非常消耗系统资源的两个过程 DB连接池HikariCP为什么如此快 原创: D ...

随机推荐

  1. spring mvc 的Controller类默认Scope是单例(singleton)的

    使用Spring MVC有一段时间了,之前一直使用Struts2,在struts2中action都是原型(prototype)的, 说是因为线程安全问题,对于Spring MVC中bean默认都是(s ...

  2. Dive into python 实例学python (2) —— 自省,apihelper

    apihelper.py def info(object, spacing=10, collapse=1): """Print methods and doc strin ...

  3. IDEA文件编码修改

    上图标注 1 所示,IDE 的编码默认是 UTF-8,Project Encoding 虽然默认是 GBK,但是一般我都建议修改为 UTF-8.上图标注 2 所示,IntelliJ IDEA 可以对 ...

  4. iOS 工程中文件变成红色是什么情况

    iOS 工程中文件变成红色是原有的文件路径改变了,系统找不到了.

  5. replication_slot and PostgreSQL Replication

    主库IP:192.168.230.128 备库IP:192.168.230.129 PostgreSQL版本: 主备机PostgreSQL源码包均位于/opt/soft_bak OS:CentOS5 ...

  6. 获取网络IP地址

    IPHostEntry iphost = Dns.GetHostEntry(txtDNS.Text);//解析并返回IPHostEntry对象 foreach (IPAddress ip in iph ...

  7. [转]IP动态切换脚本

    因为公司办公室要设置固定IP才行,而家里的IP段和公司是不一样的,家里采用了DHCP机制,这样每次就得改IP设置,很是不方便,就写了这个脚本来动态切换,很流畅的说!WINXP,WIN7测试通过~嘿嘿~ ...

  8. 阿里 drds 分布式数据库分节点查询

    mybatis 模式下,xml 中写法 <select id="selectFailDetailOneNode" resultMap="BaseResultMap& ...

  9. java 字符串转json,json转对象等等...

    @RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @Resp ...

  10. Ubuntu + CentOS7 搭建tftp Server

    基于Ubuntu系统做的tftp服务器,基于CentOS 7都差不多,书写了关键命令,测试过Ubuntu 12.0.4 和CentOS 7环境 1.介绍tftp服务器     TFTP(Trivial ...