方法一:

package   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的两种使用方法的更多相关文章

  1. angular2系列教程(十)两种启动方法、两个路由服务、引用类型和单例模式的妙用

    今天我们要讲的是ng2的路由系统. 例子

  2. git两种合并方法 比较merge和rebase

    18:01 2015/11/18git两种合并方法 比较merge和rebase其实很简单,就是合并后每个commit提交的id记录的顺序而已注意:重要的是如果公司用了grrit,grrit不允许用m ...

  3. 两种Ajax方法

    两种Ajax方法 Ajax是一种用于快速创建动态网页的技术,他通过在后台与服务器进行少量的数据交换,可以实现网页的异步更新,不需要像传统网页那样重新加载页面也可以做到对网页的某部分作出更新,现在这项技 ...

  4. mysql in 的两种使用方法

    简述MySQL 的in 的两种使用方法: 他们各自是在 in keyword后跟一张表(记录集).以及在in后面加上字符串集. 先讲后面跟着一张表的. 首先阐述三张表的结构: s(sno,sname. ...

  5. C#中的两种debug方法

    这篇文章主要介绍了C#中的两种debug方法介绍,本文讲解了代码用 #if DEBUG 包裹.利用宏定义两种方法,需要的朋友可以参考下   第一种:需要把调试方法改成debug代码用 #if DEBU ...

  6. Service的两种启动方法

    刚才看到一个ppt,介绍service的两种启动方法以及两者之间的区别. startService 和 bindService startService被形容为我行我素,而bindService被形容 ...

  7. jQuery ajax调用后台aspx后台文件的两种常见方法(不是ashx)

    在asp.net webForm开发中,用Jquery ajax调用aspx页面的方法常用的有两种:下面我来简单介绍一下. [WebMethod] public static string SayHe ...

  8. android studio gradle 两种更新方法更新

    android studio gradle 两种更新方法更新 第一种.Android studio更新 第一步:在你所在项目文件夹下:你项目根目录gradlewrappergradle-wrapper ...

  9. iOS学习——UITableViewCell两种重用方法的区别

    今天在开发过程中用到了UITableView,在对cell进行设置的时候,我发现对UITableViewCell的重用设置的方法有如下两种,刚开始我也不太清楚这两种之间有什么区别.直到我在使用方法二进 ...

随机推荐

  1. 将webservice封装成dll

    生成dll文件的步骤如下:1.发布完成后,在浏览器中打开WebService文件,如:http://localhost/WebSer/WebService1.asmx,可以看到WebService1. ...

  2. Maximo 7.5 集成方式 去掉主菜单

    最近有人在QQ中问我,在与portal集成时,客户不想显示maximo的主菜单,以前也有过朋友问过我这个问题,被我忽悠过去了,现在想起来,是不对的! 经过一翻查找,发现maximo有一个参数,port ...

  3. Map三种遍历方式

    Map三种遍历方式 package decorator; import java.util.Collection; import java.util.HashMap; import java.util ...

  4. jquery checkbox 实现单选

    最近在用javascript的时候发现网上实现checkbox单选的代码都已经过时了. 用着几年前的代码发现根本不行了 原因是jquery api已经更改 http://api.jquery.com/ ...

  5. 编写高质量ios-之一 OC 语言的起源

    要点 Objective-c为C语言添加了面向对象的特性,是其超级.Objective-c使用动态绑定的消息结构,也就是说,在运行时才会检查对象类型.接受一条消息之后,究竟应执行何种代码,由运行期环境 ...

  6. Java RandomAccessFile用法

    RandomAccessFile RandomAccessFile是用来访问那些保存数据记录的文件的,你就可以用seek( )方法来访问记录,并进行读写了.这些记录的大小不必相同:但是其大小和位置必须 ...

  7. NSDate和NSDateFormatter 相关应用代码示例

    此方法用来计算当前时间与目标时间的先后顺序: -(NSDate *)calculateTimeWithCurrentTime:(NSDate *)currentDate{ //将当前时间转为本地时区 ...

  8. Life is short

    相信不少码农曾看过类似“life is short, use Python”等之类略带调侃意味的小段子(譬如我),而其也并非不无道理.每门编程语言都是合理的存在,都有它们的优点,及缺陷. 码农们也大多 ...

  9. Java进行post和get传参数

    http://www.cnblogs.com/zhuawang/archive/2012/12/08/2809380.html get和post方法 import java.io.BufferedRe ...

  10. android 命名 数组 所有国家 String[] COUNTRIES

    static final String[] COUNTRIES = new String[] { "Afghanistan", "Albania", " ...