测试c3p0数据连接池的使用----简单笔记

加入jar包
上代码


配置文件


<c3p0-config>
  
  <named-config name="myconfig"> 

    
    <property name="driverClass">com.mysql.jdbc.Driverproperty>
    <property name="jdbcUrl">jdbc:mysql://localhost/student?useSSL=falseproperty>
    <property name="user">rootproperty>
    >


    >
    <property name="acquireIncrement">5property>
    <property name="initialPoolSize">10property>
    <property name="minPoolSize">5property>
    <property name="maxPoolSize">50property>

    
    <property name="maxStatements">20property> 
    <property name="maxStatementsPerConnection">5property>

  named-config>
c3p0-config>
package top.demo.test;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.*;

public class TestC3p0 {

    public static void main(String argv[]) throws PropertyVetoException, SQLException {

        test2();


    }


    public static void test1() throws PropertyVetoException, SQLException{

        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver            
        cpds.setJdbcUrl( "jdbc:mysql://localhost/student?useSSL=false" );
        cpds.setUser("root");                                  
        cpds.setPassword("");    

        //设置池 初始化大小
        cpds.setInitialPoolSize(5);
        Connection con =cpds.getConnection();


        System.out.println(con);

    }

    //c3p0可以使用配置文件加载配置 但是推荐使用xml加载配置
    public static void test2() throws SQLException {
        //在src下建立c3p0-config.xml 文件 文件名必须这样写
        //有了xml配置文件 new 的使用写 在    填写的name即可
        ComboPooledDataSource dataSources= new ComboPooledDataSource("myconfig");
        Connection con =dataSources.getConnection();
        System.out.println(con);
    }


}

你可能感兴趣的:(JavaEE)