druid数据库连接池

1.引用两文件(druid-1.0.9.jar和druid.properties)

注:这里的mysql-connector的jar包也需要应用进去

 

2.代码

1)DrudiDemo

import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;

public class DruidDemo {
    public static void main(String args[]) throws Exception {
        //导入jar包
        //定义配置文件
        //加载配置文件
        Properties pro = new Properties();
        InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties");
        pro.load(is);
        //获取连接池对象
        DataSource ds = DruidDataSourceFactory.createDataSource(pro);
        //获取连接
        Connection conn = ds.getConnection();
        System.out.println(conn);
    }
}

 

2)druid.properties

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/JavaDB?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC
username=root
password=root
#初始化链接数量
initialSize=5
#最大连接数
maxActive=10
#超时时间
maxWait=3000
maxIdle=8
minIdle=3

 

3.结果:

 

 4.补充帮助类

1)JDBCUtils.java

package util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

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

public class JDBCUtils {
    //定义成员变量 DataSource
    private static DataSource ds;

    static{
        //加载配置文件
        Properties pro = new Properties();
        try {
            pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
            //获取 DataSource
            ds = DruidDataSourceFactory.createDataSource(pro);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //获取连接
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    //释放资源
    public static void  close(Statement smt,Connection conn){
        close(null,smt,conn);
    }

    //释放资源
    public static void  close(ResultSet rs, Statement smt, Connection conn){
        if(rs != null){
            try {
                rs.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(smt != null){
            try {
                smt.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if(conn != null){
            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    //获取连接池方法
    public static DataSource getDataSource(){
        return ds;
    }
}

2)运行

import util.JDBCUtils;

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

public class DruidDemo2 {
    public static void main(String[] args){
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            //获取链接
            conn = JDBCUtils.getConnection();
            //定义sql
            String sql = "insert into user (`Name`, `Age`) VALUES (?, ?);";
            //获取pstmt对象
            pstmt = conn.prepareStatement(sql);
            //赋值
            pstmt.setString(1,"dzw2");
            pstmt.setInt(2,30);
            //执行
            int count = pstmt.executeUpdate();
            System.out.println(count);

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtils.close(pstmt,conn);
        }
    }
}

3)结果:

 

 

 

 

参阅:https://www.bilibili.com/video/BV1vk4y117fU?p=519

https://www.bilibili.com/video/BV1vk4y117fU?p=520

 

posted @ 2022-04-06 09:25  蜗牛的礼物  阅读(79)  评论(0编辑  收藏  举报