C3P0的两种使用方法
import java.sql.Connection;
import java.sql.SQLException;
import java.beans.PropertyVetoException;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DBPool{
private static DBPool dbPool;
private ComboPooledDataSource dataSource;
static {
dbPool=new DBPool();
}
public DBPool(){
try {
dataSource=new ComboPooledDataSource();
dataSource.setUser( "id ");
dataSource.setPassword( "pw ");
dataSource.setJdbcUrl( "jdbc:mysql://127.0.0.1:3306/test?
autoReconnect=true&useUnicode=true&characterEncoding=GB2312 ");
dataSource.setDriverClass( "com.mysql.jdbc.Driver ");
dataSource.setInitialPoolSize(2);
dataSource.setMinPoolSize(1);
dataSource.setMaxPoolSize(10);
dataSource.setMaxStatements(50);
dataSource.setMaxIdleTime(60);
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
public final static DBPool getInstance(){
return dbPool;
}
public final Connection getConnection() {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException( "无法从数据源获取连接 ",e);
}
}
public static void main(String[] args) throws SQLException {
Connection con = null;
try {
con = DBPool.getInstance().getConnection();
} catch (Exception e){
} finally {
if (con != null)
con.close();
}
}
}
原来不知道使用c3p0 是如此的简单,我一直使用properties 文件去配置c3p0,但总是连接不上数据库,后来调试才发现ComboPooledDataSource 这个对象的属性没有被设置成功,我是先获取了properties文件的内容,封装在一个 Properties对象里面,然后直接调用 ComboPooledDataSource 的 setProperties(Properties p) 方法来配置c3p0,程序是没有报错,但连不上数据库,调试发现属性都没有设置成功,只是properties这个属性被设置了而已,结果我对每个属性调用set方法后就连接上了。。。
public final class ConnectionManager {
private static ConnectionManager instance;
public ComboPooledDataSource ds;
private static String c3p0Properties = "c3p0.properties";
private ConnectionManager() throws Exception {
Properties p = new Properties();
p.load(this.getClass().getResourceAsStream(c3p0Properties));
ds = new ComboPooledDataSource();
ds.setUser(p.getProperty("user"));
ds.setPassword(p.getProperty("user"));
ds.setJdbcUrl(p.getProperty("user"));
ds.setDriverClass(p.getProperty("user"));
ds.setInitialPoolSize(Integer.parseInt(p.getProperty("initialPoolSize")));
ds.setMinPoolSize(Integer.parseInt(p.getProperty("minPoolSize")));
ds.setMaxPoolSize(Integer.parseInt(p.getProperty("maxPoolSize")));
ds.setMaxStatements(Integer.parseInt(p.getProperty("maxStatements")));
ds.setMaxIdleTime(Integer.parseInt(p.getProperty("maxIdleTime")));
}
public static final ConnectionManager getInstance() {
if (instance == null) {
try {
instance = new ConnectionManager();
} catch (Exception e) {
e.printStackTrace();
}
}
return instance;
}
public synchronized final Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
protected void finalize() throws Throwable {
DataSources.destroy(ds); // 关闭datasource
super.finalize();
}
}
如此就可以获取connection来做jdbc操作了:
Connection conn=ConnectionManager.getInstance().getConnection();
记得使用完后调用close方法:
conn.close();
c3p0 的某些参数的配置以及意义见另外一篇文章http://kangzye.blog.163.com/blog/static/368192232010442162576/
C3P0的两种使用方法的更多相关文章
- angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用
今天我们要讲的是ng2的路由系统. 例子
- git两种合并方法 比较merge和rebase
18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...
- 两种Ajax方法
两种Ajax方法 Ajax是一种用于快速创建动态网页的技术,他通过在后台与服务器进行少量的数据交换,可以实现网页的异步更新,不需要像传统网页那样重新加载页面也可以做到对网页的某部分作出更新,现在这项技 ...
- mysql in 的两种使用方法
简述MySQL 的in 的两种使用方法: 他们各自是在 in keyword后跟一张表(记录集).以及在in后面加上字符串集. 先讲后面跟着一张表的. 首先阐述三张表的结构: s(sno,sname. ...
- C#中的两种debug方法
这篇文章主要介绍了C#中的两种debug方法介绍,本文讲解了代码用 #if DEBUG 包裹.利用宏定义两种方法,需要的朋友可以参考下 第一种:需要把调试方法改成debug代码用 #if DEBU ...
- Service的两种启动方法
刚才看到一个ppt,介绍service的两种启动方法以及两者之间的区别. startService 和 bindService startService被形容为我行我素,而bindService被形容 ...
- jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)
在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...
- android studio gradle 两种更新方法更新
android studio gradle 两种更新方法更新 第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper ...
- iOS学习——UITableViewCell两种重用方法的区别
今天在开发过程中用到了UITableView,在对cell进行设置的时候,我发现对UITableViewCell的重用设置的方法有如下两种,刚开始我也不太清楚这两种之间有什么区别.直到我在使用方法二进 ...
随机推荐
- javax mail网址
http://www.oracle.com/technetwork/java/javamail/faq/index.html#tomcatconfig
- vueJs+webpack单页面应用--vue-router配置
vue-route版本要跟vue版本同步,我的vue用的2.0+的,vue-router 也用了最新版2.1+ npm安装vue-router: $ npm install vue-router -- ...
- easyui dialog iframe
function toGrant(obj,url,showMsg) { var dialog=$('#dlg_grant' ...
- Xamarin.Forms ListView点击按钮刷新最新数据
最近在研究Xamarin的东西,做到ListView遇到了一些瓶颈,像在数据庞大的情况下,该怎么针对ListView中的数据分组呢? 基于能力有限的问题,暂时写了一个只可以实现功能的临时解决方案,毕竟 ...
- 瘋子C语言笔记(指针篇)
指针篇 1.基本指针变量 (1)定义 int i,j; int *pointer_1,*pointer_2; pointer_1 = &i; pointer_2 = &j; 等价于 i ...
- [原]在AMD机器上使用android studio
amd机器上使用android studio进行调试.因为amd的cpu不支持IntelVT,所以可能会有如下错误提示 我们有几个选择: 1. 使用真机调试 2. 配合genymotion 3. ...
- 系统建设 > 医疗集团CRM系统建设步骤与分析
概述 医院客户关系管理系统(Customer Relationship Management,简称CRM)是一个完善的“以病人为中心”的管理系统,为集团/医院/总院分院/管理机构提供院前.院中.院后的 ...
- term2 配置
item2是mac下非常好用的一款终端.但默认的配色实在不好用,经过一翻搜索终于找到了比较满意的. 1.先要修改~/.bash_profile.代码如下 2.选择喜欢的配色方案. 在Preferenc ...
- Oculus中OVRPlayerController飞行视角的制作
最近项目上的事,忙的不可开交.忙里偷闲,记录下Oculus飞行视角的制作 师兄给我的要求是,带上Oculus,通过remote和头盔操作,可以完成飞行我的解决办法:1.消除重力的影响,如同在真空中,就 ...
- 【转载】我眼中的Oracle性能优化
我眼中的Oracle性能优化 大家对于一个业务系统的运行关心有如下几个方面:功能性.稳定性.效率.安全性.而一个系统的性能有包含了网络性能.应用性能.中间件性能.数据库性能等等. 今天从数据库性能的角 ...