数据库连接池的使用

数据库连接池

开源框架的使用步骤

  1. 导jar包
  2. 看帮助。
  3. 调用方法,并使用。

Druid连接池

druid.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/girls?rewriteBatchedStatements=true
username=root
password=123456
initialSize=10
minIdle=5
maxActive=20
maxWait=5000
import com.alibaba.druid.pool.DruidDataSourceFactory;

import org.junit.Test;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;

public class TestDataSource {
    @Test
    public void testDataSource() throws Exception{
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\druid.properties"));

        //1.创建一个指定参数的数据库连接池
        DataSource ds = DruidDataSourceFactory.createDataSource(properties);
        //2.从数据库连接池中获取可用的连接对象
        Connection connection = ds.getConnection();

        System.out.println("连接成功");
        //3.关闭连接
        connection.close();

    }
}

C3P0连接池

 //测试C3P0数据库连接池
    //方式1:
    @Test
    public void testC3P0() throws Exception {
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql://localhost:3306/girls");
        cpds.setUser("root");
        cpds.setPassword("123456");
        cpds.setInitialPoolSize(10);
        cpds.setMaxPoolSize(50);

        Connection connection = cpds.getConnection();

        System.out.println("连接成功!");
        connection.close();
    }

方式二:需要用到配置文件

//测试C3P0数据库连接池
    //方式2:
    @Test
    public void testC3P02() throws Exception {
        ComboPooledDataSource cpds = new ComboPooledDataSource("hello");

        Connection connection = cpds.getConnection();

        System.out.println("连接成功!");
        connection.close();
    }

配置文件:

<c3p0-config>
  <!-- 使用默认的配置读取连接池对象,首尾要定义 default-config -->
  <named-config name="hello">
      <!--  连接参数 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/girls</property>
    <property name="user">root</property>
    <property name="password">123456</property>
    
    <!-- 连接池参数 -->
    <!--初始化申请的连接数量-->
    <property name="initialPoolSize">5</property>
    <!--最大的连接数量-->
    <property name="maxPoolSize">10</property>
    <!--超时时间-->
    <property name="checkoutTimeout">3000</property>
  </named-config>

  <named-config name="otherc3p0"> 
    <!--  连接参数 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost:3306/studb</property>
    <property name="user">root</property>
    <property name="password">123456</property>
    
    <!-- 连接池参数 -->
    <property name="initialPoolSize">5</property>
    <property name="maxPoolSize">8</property>
    <property name="checkoutTimeout">1000</property>
  </named-config>
</c3p0-config>

通过数据库连接池获取连接对象

新的JDBCUtils类

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * 封装JDBCUtils
 * 通过Druid数据库连接池获取连接对象
 */
public class JDBCUtilsByDruid {
    static DataSource ds;
    static {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\druid.properties"));

            //1.创建一个指定参数的数据库连接池
            ds = DruidDataSourceFactory.createDataSource(properties);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //创建连接
    public static Connection getConnection() throws Exception{
        //2.从数据库连接池中获取可用的连接对象
//        Connection connection = ds.getConnection();
        return ds.getConnection();
    }

/**
 * 功能:释放资源
 *
 * @param set
 * @param statement
 * @param connection
 * @throws Exception
 */
    //关闭连接
    public static void close(ResultSet set, PreparedStatement statement, Connection connection) throws Exception {
        if (set != null) {
            set.close();
        }
        if (statement != null) {
            statement.close();
        }
        if (connection != null) {
            connection.close();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值