用C3P0获取连接对象的方法

package gxa.bj.util;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class C3P0Connect {
private static DataSource ds;
// 在线程中创建Connection对象的副本
private static ThreadLocal<Connection> t=new ThreadLocal<Connection>();
static{
ds=new ComboPooledDataSource();
}
/**
* @return 获取连接对象的方法
* @throws SQLException
*/
public static Connection getConnection() throws SQLException{
Connection con=t.get();//得到一个线程变量的副本
if(con==null){
con = ds.getConnection();
t.set(con);//创建一个线程变量的副本
}
return con;
}
/**关闭连接对象的方法
* @throws SQLException
*/
public static void closeConnection() throws SQLException{
Connection con=t.get();
if(con!=null){
con.close();
t.remove();
}
}
}

原文地址:https://www.cnblogs.com/tutuwowo/p/5582025.html