日期:2014-05-16  浏览次数:20448 次

apache 的DBCP实现连接池

import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DataSourceConnectionFactory;

public class testDBPool {
??? private static BasicDataSource bds = null;
??? private static ConnectionFactory cf = null;
??? public testDBPool(){
??? }
??? static{
??????? bds = new BasicDataSource();
??????? bds.setDriverClassName("com.mysql.jdbc.Driver");
??????? bds.setUrl("jdbc:mysql://localhost/wolf?useUnicode=true&characterEncoding=utf-8");
??????? bds.setInitialSize(1000);
??????? bds.setMaxActive(40);
??????? bds.setMaxWait(1000*60);
??????? bds.setUsername("root");
??????? bds.setPassword("sinianlang");
??????? cf = new DataSourceConnectionFactory(bds);
??? }
??? public static Connection getConnection() throws SQLException{
??????? return cf.createConnection();
??? }
??? public static void destroy() throws SQLException{
??????? cf = null;
??????? bds.close();
??????? bds = null;
??? }
}