c3p0和 dbcp的 两种使用方式(MySql 8.0+ 版)

文章目录

    • 前置知识:
    • dbcp:
      • 方式一:通过硬代码进行实现:
      • 方式二: 通过 Properties 文件进行配置:
    • c3p0:
      • 方式一: 通过硬代码的方式:
      • 方式二: 通过 cp30-config.xml 配置文件进行创建连接池:
    • 后记:

前置知识:

1、Mysql jdbc URL连接参数useSSL、serverTimezone 相关问题。

2、不容错过的 Java 连接池 详解。

dbcp:

方式一:通过硬代码进行实现:

package rj.util;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

public class DBCPDemo {


    public static DataSource getDataSourceWithDBCP() {
        BasicDataSource dbcp = new BasicDataSource();
        // 手动设置参数
        dbcp.setDriverClassName("com.mysql.cj.jdbc.Driver");
        // 注意,8.0+ 版的 MySql 连接时需要加上时区和字符编码
        dbcp.setUrl("jdbc:mysql://localhost:3306/sqltest?serverTimezone=GMT%2B8");
        dbcp.setUsername("root");
        dbcp.setPassword("root");
        dbcp.setInitialSize(20);
        dbcp.setMaxActive(10);

        return dbcp;
    }


    public static void main(String[] args) throws Exception {

        System.out.println(getDataSourceWithDBCP().getConnection());

    }
}

结果:
在这里插入图片描述

方式二: 通过 Properties 文件进行配置:

dbcpconfig.properties文件:

	driverClassName=com.mysql.cj.jdbc.Driver
	url=jdbc:mysql://localhost:3306/sqltest?serverTimezone=GMT%2B8
	username=root
	password=root
	initialSize=20

测试:

package rj.util;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
import java.util.Properties;

public class DBCPDemo {


    // 方式一
    public static DataSource getDataSourceWithDBCP() {
        BasicDataSource dbcp = new BasicDataSource();
        // 手动设置参数
        dbcp.setDriverClassName("com.mysql.cj.jdbc.Driver");
        // 注意,8.0+ 版的 MySql 连接时需要加上时区和字符编码
        dbcp.setUrl("jdbc:mysql://localhost:3306/sqltest?serverTimezone=GMT%2B8");
        dbcp.setUsername("root");
        dbcp.setPassword("root");
        dbcp.setInitialSize(20);
        dbcp.setMaxActive(10);

        return dbcp;
    }

    // 方式二
    public static DataSource getDataSourceWithDBCPByProperties() throws Exception {
        DataSource dbcp = null;

        // 加载配置文件时需要先有一个 Properties 对象
        Properties properties = new Properties();

        // load时需要一个流的对象,所以需要转换成流
        InputStream input = new DBCPDemo().getClass().getClassLoader().getResourceAsStream("dbcpconfig.properties");

        /**
         * Reads a property list (key and element pairs) from the input
         * byte stream. The input stream is in a simple line-oriented
         * format as specified in
         * {@link #load(java.io.Reader) load(Reader)} and is assumed to use
         * the ISO 8859-1 character encoding; that is each byte is one Latin1
         * character. Characters not in Latin1, and certain special characters,
         * are represented in keys and elements using Unicode escapes as defined in
         * section 3.3 of
         * The Java™ Language Specification.
         * 

* The specified stream remains open after this method returns. * * @param inStream the input stream. * @exception IOException if an error occurred when reading from the * input stream. * @throws IllegalArgumentException if the input stream contains a * malformed Unicode escape sequence. * @since 1.2 */ properties.load(input); // 通过配置文件创建数据源, dbcp = BasicDataSourceFactory.createDataSource(properties); return dbcp; } public static void main(String[] args) throws Exception { // System.out.println(getDataSourceWithDBCP().getConnection()); // 跟连接池建立连接 System.out.println(getDataSourceWithDBCPByProperties().getConnection()); } }

效果:
在这里插入图片描述

c3p0:

方式一: 通过硬代码的方式:

package rj.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.SQLException;

public class C3P0Demo {

    // 手动添加参数创建连接池
    public static DataSource getDataSourceWithC3P0() throws PropertyVetoException {
        ComboPooledDataSource c3p0 = new ComboPooledDataSource();
        c3p0.setDriverClass("com.mysql.cj.jdbc.Driver");
        c3p0.setJdbcUrl("jdbc:mysql://localhost:3306/sqltest?serverTimezone=GMT%2B8");
        c3p0.setUser("root");
        c3p0.setPassword("root");

        return c3p0;
    }


    public static void main(String[] args) throws PropertyVetoException, SQLException {
        System.out.println(getDataSourceWithC3P0().getConnection());
//        System.out.println(getDateSourceWithC3P0ByConfig().getConnection());
    }
}

效果:
在这里插入图片描述

方式二: 通过 cp30-config.xml 配置文件进行创建连接池:

cp30-config.xml



<c3p0-config>
    
    
    <default-config>
        <property name="user">rootproperty>
        <property name="password">rootproperty>
        <property name="driverClass">com.mysql.cj.jdbc.Driverproperty>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/sqltest?&useSSL=false&serverTimezone=UTCproperty>

    default-config>

    
    
        <property name="user">rootproperty>
        <property name="password">rootproperty>
        <property name="driverClass">com.mysql.cj.jdbc.Driverproperty>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/sqltest?&useSSL=false&serverTimezone=UTCproperty>

    named-config>


c3p0-config>

测试:

package rj.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
import java.sql.SQLException;

public class C3P0Demo {

    // 手动添加参数创建连接池
    public static DataSource getDataSourceWithC3P0() throws PropertyVetoException {
        ComboPooledDataSource c3p0 = new ComboPooledDataSource();
        c3p0.setDriverClass("com.mysql.cj.jdbc.Driver");
        c3p0.setJdbcUrl("jdbc:mysql://localhost:3306/sqltest?serverTimezone=GMT%2B8");
        c3p0.setUser("root");
        c3p0.setPassword("root");

        return c3p0;
    }


    // 通过配置文件的方式创建一个连接池
    public static DataSource getDateSourceWithC3P0ByConfig() {

        // 会根据指定的配置名进行创建连接池,如果没有指定配置名,则采用默认的
        ComboPooledDataSource c3p0 = new ComboPooledDataSource("rj");
        return c3p0;
    }

    public static void main(String[] args) throws PropertyVetoException, SQLException {
        System.out.println("方式一:"+getDataSourceWithC3P0().getConnection());
        System.out.println("方式二"+getDateSourceWithC3P0ByConfig().getConnection());
    }
}

效果:
c3p0和 dbcp的 两种使用方式(MySql 8.0+ 版)_第1张图片

后记:

有问题的小伙伴欢迎留言,共同进步。
嘻嘻,如果对您有帮助,是我的荣幸,记得点个赞哦!

你可能感兴趣的:(Mysql,dbcp,c3p0,jdbc,java,mysql)