/*
* 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. 生成arff文件,csv转为arff

    一.什么是arff格式文件 1.arff是Attribute-Relation File Format缩写,从英文字面也能大概看出什么意思.它是weka数据挖掘开源程序使用的一种文件模式.由于weka ...

  2. CommonJS规范

    CommonJS是一种规范,NodeJS是这种规范的实现.CommonJS是一 个不断发展的规范,计划将要包括如下部分: Modules Binary strings and buffers Char ...

  3. 创建纯洁的TableViewCell

    -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPat ...

  4. fzuoj Problem 2177 ytaaa

    http://acm.fzu.edu.cn/problem.php?pid=2177 Problem 2177 ytaaa Accept: 113    Submit: 265Time Limit: ...

  5. Java基础(45):冒泡排序的Java封装(完整可运行)

    1.冒泡排序 package lsg.ap.bubble; import java.util.*; public class BubbleSort { public static void bubbl ...

  6. bzoj4152 [AMPPZ2014]The Captain

    最短路,先将x排序,然后把排序后权值相邻的点连边,再把y排序,也把权值相邻的点连边,求一遍1到n的最短路就好啦. 代码 #include<cstdio> #include<queue ...

  7. WM (Constants)

    Create page WM (Constants)   Summary WM_* Constants and their definitions or descriptions and what c ...

  8. 夺命雷公狗---DEDECMS----3快速入门之隐藏

    如果我们在工作的时候遇到上操蛋的老板,本来公司是做医疗器械的,但是老板突然老了句我们不做医疗了,我们该做电影网,那么我们可以先将原本的栏目进行修改成隐藏栏目, 主要是预防变态老板突然来句“电影网更不好 ...

  9. 50136142WXY的百度地图

    50136142WXY的百度地图 [试题描述] 社团里的WXY童鞋今天要出去旅游啦(他现在在北京为1号城市),决定去朝鲜平壤(N号城市).但是很穷的WXY没有钱坐飞机,只好坐汽车去朝鲜了.但是车上的百 ...

  10. Java应用程序的打包和发布

    Java应用程序的打包和发布 简化Java应用程序的打包和发布 发布Java应用程序时Java提供了一系列打包和发布工具,可以显著的简化发布过程 该文章提供了打包Java code的几种方法,探讨Ja ...