【JDBC】Druid(德鲁伊)连接池

Druid(德鲁伊)

Druid(德鲁伊)是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池。(经历过双十一的考验)
在功能、性能、扩展性方面,都超过其他数据库连接池,同时加入了日志监控,可以很好的监控DB池连接和SQL的执行情况。

创建项目 导入 jar包

在这里插入图片描述

导入配置文件 druid.properties

  • 是properties形式的
  • 可以叫任意名称,可以放在任意目录下,我们统一放到 resources资源目录
    在这里插入图片描述
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/db?characterEncoding=UTF-8
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000

1. 编写工具类

  • 获取数据库连接池对象
    • 通过工厂来来获取 DruidDataSourceFactory类的createDataSource方法
    • createDataSource(Properties p) 方法参数可以是一个属性集对象

1.1 工具类实例:

import com.alibaba.druid.pool.DruidDataSourceFactory;

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

public class DruidUtils {
    
    
    //1.定义成员变量
    public static DataSource dataSource;

    //2.静态代码块
    static {
    
    
        try {
    
    
            //3. 创建属性集对象
            Properties p = new Properties();
            //4. 加载配置文件 Druid 连接池不能够主动加载配置文件 ,需要指定文件
            InputStream inputStream = DruidUtils.class.getClassLoader().getResourceAsStream("druid.properties");

            //5. 使用Properties对象的 load方法 从字节流中读取配置信息
            p.load(inputStream);

            //6. 通过工厂类获取连接池对象
            dataSource = DruidDataSourceFactory.createDataSource(p);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } catch (Exception e) {
    
    
            e.printStackTrace();
        }
    }

    /**
     * 获取连接的方法
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
    
    
        return dataSource.getConnection();
    }
    /**
     * 释放资源方法
     * @param con
     * @param statement
     */
    public static void close(Connection con, Statement statement){
    
    
        if(con != null && statement != null){
    
    
            try {
    
    
                statement.close();
                //归还连接
                con.close();
            } catch (SQLException e) {
    
    
                e.printStackTrace();
            }
        }
    }

    /**
     * 释放资源方法
     * @param con
     * @param statement
     * @param resultSet
     */
    public static void close(Connection con, Statement statement, ResultSet resultSet){
    
    
        if(con != null && statement != null && resultSet != null){
    
    
            try {
    
    
                resultSet.close();
                statement.close();
                //归还连接
                con.close();
            } catch (SQLException e) {
    
    
                e.printStackTrace();
            }
        }
    }
}

测试:
需求查询薪资在3eee 到5000之间的员工的姓名

package com.cyh.testpool;

import com.cyh.utils.DruidUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class TestDruid {
    
    
    //需求查询薪资在3eee 到5000之间的员工的姓名
    public static void main(String[] args) throws SQLException {
    
    
        //1. 获取连接
        Connection con = DruidUtils.getConnection();

        //2. 获取Statement对象
        Statement statement = con.createStatement();

        ResultSet resultSet = statement.executeQuery("select ename from employee where salary " +
                "between 3000 and 5000");

        //4.处理结果集
        while(resultSet.next()){
    
    
            String ename = resultSet.getString("ename");
            System.out.println(ename);
        }
        //5.释放资源
        DruidUtils.close(con,statement,resultSet);

    }
}

猜你喜欢

转载自blog.csdn.net/Guai_Ka/article/details/113750580