6. 数据库连接池技术

本文详细介绍了数据库连接池的原理和重要性,通过实例展示了C3P0、DBCP和Druid三种数据库连接池的配置方法,包括Java代码配置和外部配置文件的方式。同时提供了数据连接池的工具类实现,以方便管理和获取数据库连接。通过对不同连接池的使用,可以有效提高数据库操作的效率和资源利用率。
摘要由CSDN通过智能技术生成

1.概述

之前没有数据库连接池,建立一个连接自身使用完就直接关闭了,不能得到很好的利用。现在连接完在放回数据库连接池中。
以前是造完汽车之后销毁,现在造完汽车后多次利用。
image.png
image.png
image.png

2.c3p0数据库连接池

2.1 获取数据库连接池

2.1.1 方式一(暴露基本信息的连接)

//    暴露基本信息的连接
    public void tset1() throws Exception {
//        获取c3p0数据库连接池
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true");
        cpds.setUser("root");
        cpds.setPassword("123456");
//        设置相关参数对数据库连接池进行管理
//        设置初始时数据库连接池中的连接数
        cpds.setInitialPoolSize(10);
        Connection conn=cpds.getConnection();
        System.out.println(conn);
//        销毁数据库连接池
//        DataSources.destroy(cpds);
    }

2.1.2 方式二(properyies配置文件)

//    使用properyies配置文件的连接
    public void test2() throws Exception {
        //        获取c3p0数据库连接池
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        ResourceBundle rb=ResourceBundle.getBundle("jdbc");
        cpds.setDriverClass(rb.getString("driverClass"));
        cpds.setJdbcUrl(rb.getString("url"));
        cpds.setUser(rb.getString("user"));
        cpds.setPassword(rb.getString("password"));
//        设置相关参数对数据库连接池进行管理
//        设置初始时数据库连接池中的连接数
        cpds.setInitialPoolSize(10);
        Connection conn=cpds.getConnection();
        System.out.println(conn);
    }
url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
user=root
password=123456
driverClass=com.mysql.jdbc.Driver

2.1.3 方式三(xml配置文件)

//    使用xml配置文件的连接
    public void test3() throws Exception {
        ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
        Connection conn=cpds.getConnection();
        System.out.println(conn);
    }
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<c3p0-config>
<!--    自定义配置文件名,当需要调用时的标识符-->
    <named-config name="helloc3p0">
<!--        四个基本的配置信息-->
<!--        配置信息名称要和cpds.set的名称一致,采用小驼峰命名法-->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true</property>
        <property name="user">root</property>
        <property name="password">123456</property>
<!--        进行数据库连接池管理的基本信息-->
<!--        当数据库连接池数量不够时,一次向数据库服务器申请的连接数量-->
        <property name="acquireIncrement">5</property>
<!--        初始时的连接数-->
        <property name="initialPoolSize">10</property>
<!--        数据库连接池维护的最少连接处-->
        <property name="minPoolSize">10</property>
<!--        数据库连接池维护的最多连接处-->
        <property name="maxPoolSize">1000</property>
<!--        数据库连接池最多维护Statements的个数-->
<!--        例如propertyStatement预编译的sql语句-->
        <property name="maxStatements">50</property>
<!--        每个用户连接最多Statements的个数-->
        <property name="maxStatementsPerConnection">5</property>
    </named-config>
</c3p0-config>

2.2 数据连接池的工具类

public class JDBCUtils2 {
//    这个需要放在外面,因为只建立一个数据库连接池
//    放在里面相当于建立一次连接,建立一个数据库连接池
    private static ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
    public static Connection getConnection() throws SQLException {
        Connection conn=cpds.getConnection();
        return conn;
    }
}

3.dbcp数据库连接池

3.1 获取数据库连接池

3.1.1 方式一

   public void test1() throws SQLException {
//        创建dbcp数据库连接池
        BasicDataSource bds=new BasicDataSource();
//        设置基本配置信息
        bds.setDriverClassName("com.mysql.jdbc.Driver");
        bds.setUrl("jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true");
        bds.setUsername("root");
        bds.setPassword("123456");
//        还可以设置其他属性
//        bds.setInitialSize(10);
//        bds.setMaxActive(10);
        Connection conn=bds.getConnection();
        System.out.println(conn);
    }

image.png

3.1.2 方式二

public void test2() throws Exception {
//        方式一
//        Properties props=new Properties();
//        InputStream fis=ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
//        props.load(fis);
//        DataSource bds= BasicDataSourceFactory.createDataSource(props);
//        Connection conn=bds.getConnection();
//        System.out.println(conn);
//        方式二
        Properties props=new Properties();
        FileInputStream fis=new FileInputStream("src/dbcp.properties");
        props.load(fis);
        DataSource bds= BasicDataSourceFactory.createDataSource(props);
        Connection conn=bds.getConnection();
        System.out.println(conn);
    }

dbcp.properties

url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver

3.2 数据连接池工具类

//这里使用静态属性和静态代码块,使得其只在类加载时初始化一次,即只建立一个数据库连接池
private static DataSource bds=null;
    static{
        try {
            Properties props=new Properties();
            FileInputStream fis=new FileInputStream("src/dbcp.properties");
            props.load(fis);
            bds= BasicDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection2() throws Exception {
        Connection conn=bds.getConnection();//从数据库连接池中获取连接
        return conn;
    }

4.Druid(德鲁伊)数据库连接池(*)

查看当前类的子类型,光标移到指定位置,ctrl+H
image.png
image.png

4.1 获取数据库连接池

配置文件方式获取

public void test1() throws Exception {
//        加载配置文件的信息
        Properties prop= new Properties();
        FileInputStream fis=new FileInputStream("src/druid.properties");
        prop.load(fis);
//        工厂根据配置文件创建数据源(数据库连接池)
        DataSource sorce=  DruidDataSourceFactory.createDataSource(prop);
//        从数据库连接池获取连接
        Connection conn=sorce.getConnection();
        System.out.println(conn);
    }

druid.properties

url=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
username=root
password=123456
driverClassName=com.mysql.jdbc.Driver

4.2 数据库连接池工具类

//类加载保证只存在一个数据库连接池
private static DataSource sorce=null;
    static{
        try {
//        加载配置文件的信息
            Properties prop= new Properties();
            FileInputStream fis=new FileInputStream("src/druid.properties");
            prop.load(fis);
//        工厂根据配置文件创建数据源(数据库连接池)
            sorce=  DruidDataSourceFactory.createDataSource(prop);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection3() throws Exception{
//        从数据库连接池获取=连接
        Connection conn=sorce.getConnection();
        return conn;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值