c3p0连接池

1导包

2编写工具类

    private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
    /**
     * 配置DataSource
     */
    public static void configDataSource(){
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/zhanghanlun");
            dataSource.setUser("zhanghanlun");
            dataSource.setPassword("123456");
            dataSource.setInitialPoolSize(3); //初始化连接个数
            dataSource.setMaxPoolSize(10);//最大链接数
            dataSource.setMinPoolSize(3);//最小链接数
            dataSource.setAcquireIncrement(3);//设置每次增加的连接数
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取Connection连接
     * @return
     */
    public static Connection getConnection(){
        Connection conn = null;
        try {

            conn = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

  

QueryRunner qr=newQueryRunner(数据源);这里的数据源,我们可以通过C3P0Utils.getDataSource() 获得。如果不使用DBUtils工具类,我们就可以直接通过C3P0Utils.getConnection()来直接获得连接。

 

posted @ 2018-11-19 15:41  元气满满01  阅读(153)  评论(0编辑  收藏  举报