本来想自己写一个,后来还是封装了一下就用了。 JDBCPool.java import java.sql.Connection; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; public class JDBCPool { private String name; private ComboPooledDataSource cpds = null; private String driverName; private String databaseUrl; private String databaseUser; private String databasePassword = "123"; private int minPoolSize = 5; private int maxPoolSize = 50; private int acquireIncrement = 5; private int initialPoolSize = 30; private int idleTestPeriod = 60; private boolean validate = true; public JDBCPool() { } public JDBCPool(String driver, String url, String user, String password) throws SqlConnectionException { driverName=driver; databaseUrl=url; databaseUser=user; databasePassword=password; init(); try { cpds.getConnection(); } catch (SQLException e) { throw new SqlConnectionException("获取连接异常,初始化可能失败",e); } } public void init() throws SqlConnectionException { try { cpds = new ComboPooledDataSource(); cpds.setMaxIdleTime(60); cpds.setDriverClass(driverName); // 驱动器 cpds.setJdbcUrl(databaseUrl); // url cpds.setUser(databaseUser); // 用户名 cpds.setPassword(databasePassword); // 密码 cpds.setInitialPoolSize(initialPoolSize); // 初始化连接池大小 cpds.setMinPoolSize(minPoolSize); // 最少连接数 cpds.setMaxPoolSize(maxPoolSize); // 最大连接数 cpds.setAcquireIncrement(acquireIncrement); // 连接数的增量 cpds.setIdleConnectionTestPeriod(idleTestPeriod); // 测连接有效的时间间隔 cpds.setTestConnectionOnCheckout(validate); // 每次连接验证连接是否可用 } catch (Exception e) { throw new SqlConnectionException("c3p0池初始化异常",e); } } public Connection getConnection() throws SqlConnectionException { Connection connection = null; try { connection = cpds.getConnection(); } catch (SQLException e) { throw new SqlConnectionException("在c3p0池中获得连接异常",e); } return connection; } public void release() throws SqlConnectionException { try { if (cpds != null) { cpds.close(); } } catch (Exception e) { throw new SqlConnectionException("释放连接"+name+" 异常",e); } } public String getName() { return name; } public void setName(String name) { this.name = name; } public ComboPooledDataSource getCpds() { return cpds; } public void setCpds(ComboPooledDataSource cpds) { this.cpds = cpds; } public String getDriverName() { return driverName; } public void setDriverName(String driverName) { this.driverName = driverName; } public String getDatabaseUrl() { return databaseUrl; } public void setDatabaseUrl(String databaseUrl) { this.databaseUrl = databaseUrl; } public String getDatabaseUser() { return databaseUser; } public void setDatabaseUser(String databaseUser) { this.databaseUser = databaseUser; } public String getDatabasePassword() { return databasePassword; } public void setDatabasePassword(String databasePassword) { this.databasePassword = databasePassword; } public int getMinPoolSize() { return minPoolSize; } public void setMinPoolSize(int minPoolSize) { this.minPoolSize = minPoolSize; } public int getMaxPoolSize() { return maxPoolSize; } public void setMaxPoolSize(int maxPoolSize) { this.maxPoolSize = maxPoolSize; } public int getAcquireIncrement() { return acquireIncrement; } public void setAcquireIncrement(int acquireIncrement) { this.acquireIncrement = acquireIncrement; } public int getInitialPoolSize() { return initialPoolSize; } public void setInitialPoolSize(int initialPoolSize) { this.initialPoolSize = initialPoolSize; } public int getIdleTestPeriod() { return idleTestPeriod; } public void setIdleTestPeriod(int idleTestPeriod) { this.idleTestPeriod = idleTestPeriod; } public boolean isValidate() { return validate; } public void setValidate(boolean validate) { this.validate = validate; } } JDBCPoolManager.java import java.util.HashMap; import java.util.Map; import javax.sql.DataSource; public class JDBCPoolManager { private static Map<String,JDBCPool> pools=new HashMap<String,JDBCPool>(); public static void addNew(String name,String driver,String url,String user,String password) throws SqlConnectionException{ JDBCPool jdbcPool=new JDBCPool(driver,url,user,password); jdbcPool.setName(name); pools.put(name,jdbcPool); } public static boolean isExistConnection(String name){ return pools.containsKey(name); } public static java.sql.Connection getConnection(String name) throws SqlConnectionException{ return pools.get(name).getConnection(); } public static DataSource getDataSource(){ return null; } public static void destroyPool(String name) throws SqlConnectionException{ try { pools.get(name).release(); } catch (SqlConnectionException e) { throw e; } finally{ //pools.get(name).getCpds().set //TODO 需要解决 } pools.remove(name); } }