jdbc操作使用的数据库c3p0 druid连接池配置

数据库连接池

数据库连接池就是一个容器,存放数据库链接的容器
当我们使用Java连接数据库时,会首先创建对数据库的链接,执行完sql语句之后再将连接释放,再执行SQL语句时就要重新申请,重新释放,创建链接的速度是很慢的,所以我们采用将数据库链接池化的方式增加代码的速度.
当使用数据库连接池之后,在系统初始化好后,链接池(容器)被创建,连接池(容器)会申请一些连接对象,当用户来访问数据库时,从容器中获取连接对象,用户访问完后,会将链接归还给容器,这样就节省了申请链接的时间,增大了代码效率.

C3P0数据库连接池的基本使用

  1. C3P0 jar包导入
  2. 定义配置文件 c3p0.properties 或者 c3p0-config.xml 自动获取
  3. 创建连接池对象
  4. 获取链接

jar包导入

C3P0 jar包下载地址 https://sourceforge.net/projects/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/database</property>
        <property name="user">root</property>
        <property name="password">mysql</property>
<!--        连接池参数-->
<!--        初始化申请的连接数量-->
        <property name="initialPoolSize">5</property>
<!--        最大连接数量-->
        <property name="maxPoolSize">10</property>
<!--        最大等待时间 ms-->
        <property name="checkoutTimeout">3000</property>
    </default-config>
<!--        指定名称的配置-->
    <named-config name="luoyudi">
        <!--        连接参数-->
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/database</property>
        <property name="user">root</property>
        <property name="passworld">mysql</property>
        <!--        连接池参数-->
        <property name="initialPoolSize">5</property>
        <property name="maxPoolSize">10</property>
        <property name="checkoutTimeout">3000</property>
    </named-config>
</c3p0-config>

获取连接池对象

//使用默认配置
DataSource dataSource = new ComboPooledDataSource();
//或者 使用指定名称的配置
DataSource dataSource = new ComboPooledDataSource("luoyudi");

获取链接

Connection connection = dataSource.getConnection();

Druid数据库连接池基本使用

  1. 导入jar包
  2. 定义配置文件 .properties文件 手动获取
  3. 加载配置文件
  4. 获取数据库连接池对象
  5. 获取链接

导入jar包

Druid jar包下载官网https://repo1.maven.org/maven2/com/alibaba/druid/

定义配置文件

diverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/db1
username=root
password=mysql
#初始化连接数量
initialSize=5
#最大连接数量
maxActive=10
#最大等待时间
maxWait=3000

加载配置文件

Properties properties = new Properties();
InputStream inputStream = test.class.getClassLoader().getResourceAsStream("druid.properties");
properties.load(inputStream);

获取链接池对象

DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);

获取链接

Connection connection = dataSource.getConnection();

jdbc工具类

为了简化我们的操作,我们需要创建一个工具类JdbcUtils
用来提供

  1. 静态代码块初始化连接池对象
  2. 获取连接方法
  3. 获取资源池方法
  4. 释放资源

这里使用的是Druid数据库连接池

package util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @author luoyudi
 */
public class JdbcUtils {

// 定义成员变量
    private static DataSource dataSource;
    static {
//        加载配置文件
        Properties properties = new Properties();
        try {
            properties.load(JdbcUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            dataSource= DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取链接
     */
    public static Connection getConnection() throws SQLException {
        return dataSource.getConnection();
    }

    /**
     * 释放资源
     */
    public static void close(Statement statement,Connection connection){
        close(null,statement,connection);
    }

    /**
     * 释放资源
     */
    public static void close(ResultSet resultSet,Statement statement, Connection connection){
        if(resultSet!=null){
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        if(statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(connection!=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 获取链接池
     */
    public static  DataSource getDataSource(){
        return dataSource;
    }

}

接下来 我们可以使用这个工具类测试一下

package cn.Druid;

import util.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 使用工具类
 *
 * @author luouydi
 */
public class DruidDemo {

    public static void main(String[] args) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;

//        获取链接
        try {
            connection = JdbcUtils.getConnection();
//            定义sql
            String sql = "insert into student values(?,?)";
//            获取pstmt对象
            preparedStatement = connection.prepareStatement(sql);
//            补全sql
            preparedStatement.setString(1, "wangwu");
            preparedStatement.setInt(2, 1000);
//            执行sql
            int count = preparedStatement.executeUpdate();
            System.out.println(count);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
//            释放资源
            JdbcUtils.close(preparedStatement, connection);
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我会回答你的问题,以下是常用数据库连接池 DBCP、c3p0Druid配置说明: 1. DBCP (Apache Commons DBCP) DBCP 是 Apache Commons 项目中的一个子项目,它是一个纯 Java 实现的数据库连接池。DBCP 的使用非常简单,只需要引入相关的 jar 包即可。以下是 DBCP 的配置说明: ``` # 最大连接数 maxActive=100 # 最大空闲连接数 maxIdle=30 # 最小空闲连接数 minIdle=10 # 获取连接时的最大等待时间,单位为毫秒 maxWait=10000 # 是否开启自动回收空闲连接的线程 removeAbandoned=true # 回收超时时间,单位为秒 removeAbandonedTimeout=180 # 是否开启缓存 PreparedStatement,提高性能 poolPreparedStatements=true # 缓存 PreparedStatement 的最大数量 maxOpenPreparedStatements=100 ``` 2. c3p0 c3p0 是一个开源的 JDBC 数据库连接池,它实现了数据源和 JNDI 绑定,支持 JDBC3 的 Connection 和 Statement 缓存以及 JDBC4 的自动化管理。以下是 c3p0配置说明: ``` # 最大连接数 c3p0.maxPoolSize=100 # 最小连接数 c3p0.minPoolSize=10 # 初始化连接数 c3p0.initialPoolSize=10 # 获取连接时的最大等待时间,单位为毫秒 c3p0.checkoutTimeout=10000 # 是否自动回收超时连接 c3p0.autoCommitOnClose=true # 是否开启自动回收空闲连接的线程 c3p0.idleConnectionTestPeriod=60 # 回收超时时间,单位为秒 c3p0.maxIdleTime=1800 # 是否开启缓存 PreparedStatement,提高性能 c3p0.cachePreparedStatements=true # 缓存 PreparedStatement 的最大数量 c3p0.maxStatements=100 ``` 3. Druid Druid 是阿里巴巴开源的一个高性能、可扩展、功能强大的数据库连接池。它主要提供了以下功能:监控统计、防御 SQL 注入、批量处理、数据源加密、日志记录等。以下是 Druid配置说明: ``` # 最大连接数 druid.maxActive=100 # 最大空闲连接数 druid.maxIdle=30 # 最小空闲连接数 druid.minIdle=10 # 获取连接时的最大等待时间,单位为毫秒 druid.maxWait=10000 # 是否开启自动回收空闲连接的线程 druid.removeAbandoned=true # 回收超时时间,单位为秒 druid.removeAbandonedTimeout=180 # 是否开启缓存 PreparedStatement,提高性能 druid.poolPreparedStatements=true # 缓存 PreparedStatement 的最大数量 druid.maxOpenPreparedStatements=100 # 是否开启 SQL 执行监控 druid.stat=true # 是否开启防御 SQL 注入功能 druid.filters=stat,wall,log4j ``` 以上就是常用数据库连接池 DBCP、c3p0Druid配置说明。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值