C3P0数据库连接池

c3p0-config.xml                                //配置文件,注意放到src下面
<c3p0-config>
  <!-- 使用默认的配置读取连接池对象 -->
  <default-config>
  	<!--  连接参数 -->
    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydatabase?&amp;useSSL=false&amp;serverTimezone=UTC</property>
    <property name="user">root</property>
    <property name="password"></property>
    
    <!-- 连接池参数 -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">10</property>
    <property name="checkoutTimeout">5000</property>
  </default-config>

  <named-config name="otherc3p0"> 
    <!--  连接参数 -->
    <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydatabase?&amp;useSSL=false&amp;serverTimezone=UTC</property>
    <property name="user">root</property>
    <property name="password"></property>
    
    <!-- 连接池参数 -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">10</property>
    <property name="checkoutTimeout">1000</property>
  </named-config>
</c3p0-config>
mysql8.0 和msql5.0驱动已经变了注意下最新的jar包
C3P0Demo1
package cn.C3P0;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class C3P0Demo1 {
    public static void main(String[] args) throws SQLException {
        //1.船舰数据库连接对象
        DataSource ds =new ComboPooledDataSource();
        //2.获取连接对象
        Connection conn =ds.getConnection();
        //3.打印
        //4.啃了半天,c3p0-config.xml要放在src报下
        System.out.println(conn);
    }
}

多个Connection连接连接池
C3P0Demo2
package cn.C3P0;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class C3P0Demo2 {
    public static void main(String[] args) throws SQLException {
        //1.获取DataSource
        DataSource ds=new ComboPooledDataSource();
        //如果指定其他数据库,参数中加<named-config name="otherc3p0">
        //加入"otherc3p0"  参考配置文件,上面的是默认的
        //2.获取连接
        for(int i=1;i<=11;i++){
            Connection conn=ds.getConnection();
            System.out.println(i+" :"+conn);
            System.out.println("---------------------");
            if(i==5){
                conn.close();//归还连接到连接池
            }
        }
    }
}

学习使用,基本都在使用Druid连接池
;