Use and configuration of C3P0-Maven project and ordinary java project

1. Configuration method

There are three ways to configure c3p0, namely

1. Provide a c3p0-config.xml file under the classpath

2. Provide a c3p0.properties file under the classpath

3.setters set each configuration item one by one

 datasource = new ComboPooledDataSource();
//连接配置
datasource.setJdbcUrl("jdbc:mysql://localhost:3306/test?serverTimezone=UTC");
datasource.setDriverClass("com.mysql.cj.jdbc.Driver");
datasource.setPassword("123");
datasource.setUser("root");

//连接池配置
datasource.setAcquireIncrement(5);//每创建的数量间隔
datasource.setInitialPoolSize(5);//初始化池的大小
datasource.setMaxPoolSize(20);//最大大小
datasource.setMinPoolSize(5);//最小大小

2. The location of the configuration file

We mainly need to pay attention to the location of the configuration file

2.1, c3p0 configuration of Maven project

1. Import c3p0 dependencies first

Add in the pom.xml file

  <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>

2. Add configuration file information in the resources folder.
Don't add it under the src file, but under the resources folder! ! !

Insert picture description here

<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
    <!-- 默认配置  创建连接池对象时,默认是加载该配置信息-->
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?serverTimezone=UTC</property>
        <property name="user">root</property>
        <property name="password">123456</property>

        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">20</property>
        <property name="minPoolSize">5</property>
        <property name="checkoutTimeout">3000</property>
    </default-config>
    <!--为oracle提供的配置 创建连接池对象时,可以指定命名加载配置信息-->

    <named-config name="oracle-config">

        <property name="jdbcUrl">jdbc:oracle:thin:@地址:端口:ORCL</property>
        <property name="driverClass">oracle.jdbc.driver.OracleDriver</property>
        <property name="user">root</property>
        <property name="password">12345</property>
        <!-- 池参数 -->
        <property name="acquireIncrement">3</property>
        <property name="initialPoolSize">30</property>
        <property name="minPoolSize">2</property>
        <property name="maxPoolSize">50</property>
    </named-config>

    <named-config name="otherc3p0">
    </named-config>
</c3p0-config>

3. Test:

 datasource = new ComboPooledDataSource();//文件位置对的话,自动加载
 public static void main(String[]args) {
    
    
        Connection connection= null;
        Statement statement = null;
        ResultSet res = null;
        try{
    
    
            System.out.println("connect to database....");
            connection = JDBCUtils_C3P0.getConnect();

            System.out.println("create statement...");
            statement = connection.createStatement();

            //3、选择sql命令操作数据库中的数据
            String sql;
            sql="select * from bank";
            //4、执行sql语句,获取结果集
            res = statement.executeQuery(sql);//获取结果集

            //5、从结果集里面获取数据
            while(res.next()){
    
    
                int id = res.getInt("id");
                String name = res.getString("name");
                System.out.println(id+":"+name);

            }
        } catch (SQLException e) {
    
    
            e.printStackTrace();
        }finally {
    
    
                JDBCUtils_C3P0.free(connection,statement,res);
        }
    }

2.2, configuration of ordinary java project

Import dependent packages and add configuration files under the src folder. (Not tried)
Package download:
link: https://pan.baidu.com/s/1rAEXqMYvLv7bU4B-oE7o5w
Extraction code: udyg
copy this content and open the Baidu Netdisk mobile app, the operation is more convenient

3. The difference between c3p0 and DBCP

  • dbcp does not automatically reclaim idle connections, but c3p0 does.
  • Frameworks such as Hibernate recommend using c3p0 as the connection pool by default, and DBCP is not supported in the future

Guess you like

Origin blog.csdn.net/qq_44861675/article/details/108165571