c3po下载地址:http://sourceforge.net/projects/c3p0/
必须加入的包:log4j 下载地址:http://jakarta.apache.org/log4j
以及相关数据库的驱动包。

示例代码:

import java.beans.PropertyVetoException; 

import java.sql.Connection; 

import java.sql.SQLException; 


import com.mchange.v2.c3p0.ComboPooledDataSource; 


public class ConnectionFactory { 


 private ConnectionFactory() { 

 } 


 private static ComboPooledDataSource cpds = null; 


 static { 

 try { 

 cpds = new ComboPooledDataSource(); 

 cpds.setDriverClass("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 

 cpds.setJdbcUrl("jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=xian_db"); 

 cpds.setUser("sa"); 

 cpds.setPassword("sa"); 

 cpds.setInitialPoolSize(2); // 初始化连接池大小 

 cpds.setMinPoolSize(1); // 最少连接数 

 cpds.setMaxPoolSize(10); // 最大连接数 

 cpds.setAcquireIncrement(1); // 连接数的增量 

 cpds.setIdleConnectionTestPeriod(3000); // 测连接有效的时间间隔 

 } catch (PropertyVetoException e) { 

 e.printStackTrace(); 

 } 

 } 


 public static synchronized Connection getConnection() { 

 Connection con = null; 

 try { 

 con = cpds.getConnection(); 

 } catch (SQLException e) { 

 e.printStackTrace(); 

 } 

 return con; 

 } 

}