2020-08-17C3P0数据库连接池与druid数据库连接池

C3P0下载地址
https://mvnrepository.com/artifact/com.mchange/c3p0

C3P0数据库连接池

  • 步骤
    ①导入jar包c3p0和mchange-commons-java不要忘记导入数据库驱动jar包
    ②定义配置文件c3p0.properties或者c3p0-config.xml,路径为src目录下
    ③创建核心对象CombopooledDataSource; DataSource dataSource = new ComboPooledDataSource();
    ④获取连接getDataSource
  • 配置文件c3p0-config.xml


    

        com.mysql.cj.jdbc.Driver
        jdbc:mysql://localhost:3306/db4?&useSSL=false&serverTimezone=GMT
        root
        545267257zzt


        10

        30

        20

        5

        200
    

    
        com.mysql.cj.jdbc.Driver
        jdbc:mysql://localhost:3306/db4?&useSSL=false&serverTimezone=GMT
        root
        545267257zzt

        10
        30
        20
        5
        200
    


import com.mchange.v2.c3p0.ComboPooledDataSource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class c3p0 {
    public static void main(String[] args) throws SQLException {
        //1.获取DataSource,使用默认配置
        DataSource dataSource = new ComboPooledDataSource();
        //使用配置文件中指定的配置信息,连接不同的数据库
        //DataSource dataSource1 = new ComboPooledDataSource("mysql");
        //2.获取连接
        Connection connection = dataSource.getConnection();
        //sql操作
        //释放连接
        connection.close();
    }
}

Druid数据库连接池

  • 步骤
  1. 导入druid-x.x.x.jar包
  2. 定义配置文件,是properties形式,可以叫任意名称,可以放置在任意路径下,手动加载
  3. 加载配置文件
    ClassLoader classLoader = druid.class.getClassLoader();
    URL resource = classLoader.getResource("druid.properties");
    String string = resource.getPath();
    properties.load(new FileReader(string));
  4. 获取数据源连接池对象:通过工厂类来获取DruidDataSourceFactory
  5. 获取连接getConnection;
  • 配置文件druid.properties
driverClassname=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/db4?useSSL=false&serverTimezone=UTC
username=root
password=545267257zzt
#初始化连接数
initialSize=5
#最大连接数
maxActive=10
#最大等待时间
maxWait=3000
import javax.sql.DataSource;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.util.Properties;

public class druid {
    public static void main(String[] args) throws Exception {
        Properties properties = new Properties();
        ClassLoader classLoader = druid.class.getClassLoader();
        URL resource = classLoader.getResource("druid.properties");
        String string = resource.getPath();
        properties.load(new FileReader(string));
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);

    }
}

定义druid的工具类

  1. 定义一个类JDBCUtils
  2. 提供静态代码块加载配置文件,初始化连接池对象
  3. 提供方法
    ①获取连接的方法:通过数据库连接池获取连接
    ②释放资源
    ③获取连接池的方法
package it.heima.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class jdbcUtil {
//    1. 定义成员变量DataSource
    private static DataSource ds;
    static {
        Properties properties = new Properties();
        ClassLoader classLoader = jdbcUtil.class.getClassLoader();
        URL resource = classLoader.getResource("druid.properties");
        String path = resource.getPath();
        try {
            properties.load(new FileReader(path));
            ds= DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
    *获取连接的方法
     */
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }
    /*
    *释放资源
     */
    public static void close(Statement statement,Connection connection){
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
    public static void close(Statement statement, Connection connection, ResultSet resultSet){
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /*
    获取连接池的方法
     */
    public static DataSource getDataSource(){
        return ds;
    }
}
public class jdbcUtilTest {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        try {
            connection = jdbcUtil.getConnection();
            String sql = "insert into student (id,name,cid) values (3, ?, 2)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"zhangzetao");
            preparedStatement.executeUpdate();
            jdbcUtil.close(preparedStatement,connection);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}

你可能感兴趣的:(2020-08-17C3P0数据库连接池与druid数据库连接池)