c3p0连接池

1. 依赖jar包: c3p0-0.9.1.2.jar

2.配置连接池

import java.sql.Connection;

import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;



public class C3P0

{

    public static  final String DBUrl="jdbc:mysql://localhost:3306/sqlesmailtrack?useUnicode=true&characterEncoding=utf8"; 

    public static final String DBUser="root"; 

    public static final String DBPassword="123434";  

    public static final String DBDriver="com.mysql.jdbc.Driver";

    

    private  static ComboPooledDataSource  dataSource;

    private static  C3P0 instance= null;

    public C3P0()

    {

        

        dataSource = new ComboPooledDataSource();

        dataSource.setUser(DBUser);

        dataSource.setPassword(DBPassword);

        dataSource.setJdbcUrl(DBUrl);

        try

        {

            dataSource.setDriverClass(DBDriver);

        }

        catch (Exception e)

        {

            e.printStackTrace();

        }

        dataSource.setInitialPoolSize(30);

        dataSource.setMaxPoolSize(100);

        dataSource.setMinPoolSize(10);

    }

    //保证单例模式

    public synchronized static C3P0 getInstance()

    {

        if(instance==null)

        {

            instance = new C3P0();

        }

        return instance;

    }

    

    public Connection  getConnection() throws SQLException

    {

        return dataSource.getConnection();

    }

    

    //测试连接

    public static void main(String[] args) throws SQLException

    {

        long begin = System.currentTimeMillis();

        for(int i=0;i<1000;i++)

        {

                 Connection  conn=    C3P0.getInstance().getConnection(); 

                 conn.close();

        }

        long end = System.currentTimeMillis();

        System.out.println(" c3p0连接池"+(end-begin));

        

        

    }

你可能感兴趣的:(c3p0)