jdbc+druid 实现数据连接池

前言:

基于springboot框架下封装了一个jdbc的数据库操作类,实现常用的增删改查功能,但是频繁创建数据库链接会消耗资源,于是找到网上找到结合druid实现数据库连接池共享的方法,步骤如下:

1.pom添加依赖:

 

 

2.在resources目录下创建一个 druid.properties配置文件,内容如下:

 

 

3.编写jdbcutils工具类,代码如下:

 
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.*;
 

/**
 * JDBC工具类 使用Durid连接池
 * 0.定义静态成员变量:DataSource
 * 1.定义静态代码块,加载配置文件,初始化连接池对象
 * 2.定义方法:
 * 	1.获取连接对象
 * 	2.释放连接
 * 	3.获取连接池方法
 * 3.使用工具类,需要自己定义sql,执行sql并处理结果
 */
public class JDBCUtils {

    private static DataSource ds ;

    static {

        try {
            //1.加载配置文件
            Properties pro = new Properties();
            //使用ClassLoader加载配置文件,获取字节输入流
            InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
            pro.load(is);

            //2.初始化连接池对象
            ds = DruidDataSourceFactory.createDataSource(pro);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接池对象
     */
    public static DataSource getDataSource(){
        return ds;
    }
    /**
     * 获取连接Connection对象
     */
    public static Connection getConnection() throws SQLException {
        return  ds.getConnection();
    }


    //关闭连接
    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        close(stmt, conn);
        if(rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    //关闭连接
    public static void close(Statement stmt,Connection conn) {
        if(conn != null) { //归还连接
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

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

    //关闭连接
    public static  void close(List<Statement> stmts, Connection conn) {
        if(conn != null) { //归还连接
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        for(Statement stmt: stmts) {
            if(stmt != null) {
                try {
                    stmt.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }






    /**
     * 判断当前sql语句返回是否为空
     *
     * @param sql
     * @return 返回true表示空数据集,返回false表示非空数据集
     * @throws SQLException
     */
    public static boolean checkIsEmpty(String sql) throws SQLException{
        Statement state = null;
        ResultSet rs = null;
        boolean isempty=true;
        Connection conn = getConnection();

        state = conn.createStatement();
        rs = state.executeQuery(sql);
        if (rs==null){
            isempty=true;
        }else{
            if(rs.next()){
                isempty=false;
            }else{
                isempty=true;
            }
        }

        //关闭链接
        close(state, conn);

        return isempty;
    }


    /**
     * 获取某个sql语句的首行首列值
     *
     * @param sql
     * @return 返回首行首列值
     * @throws SQLException
     */
    public static String getTopValue(String sql) throws SQLException{
        Statement state = null;
        ResultSet rs = null;
        String topvalue="";
        Connection conn = getConnection();

        state = conn.createStatement();
        rs = state.executeQuery(sql);
        if (rs!=null){
            if(rs.next()){
                topvalue = rs.getString(1);
            }
        }

        //关闭链接
        close(state, conn);

        return topvalue;
    }

    /**
     * 获取某个sql语句的首行记录集
     *
     * @param sql
     * @return 返回首行记录集
     * @throws SQLException
     */
    public static ResultSet getTopResultSet(Connection conn, Statement state, String sql)throws SQLException {
        //Statement state = null;
        ResultSet rs = null;
        //Connection conn = getConnection();

        //state = conn.createStatement();

        rs = state.executeQuery(sql);
        if (rs!=null){
            rs.next();
        }

        //关闭链接,不能关闭,否则就获取不到数据集的内容了
        //close(state, conn);

        return rs;
    }


    /**
     * 执行某个sql语句的更新操作
     *
     * @param sql
     * @return 执行成功返回true,失败返回false
     * @throws SQLException
     */
    public static boolean execSql(String sql)throws SQLException {
        PreparedStatement  pstmt = null;
        int iflag=-1;
        boolean res=false;
        Connection conn = getConnection();

        pstmt = conn.prepareStatement(sql);
        iflag = pstmt.executeUpdate();
        res = true;

        //关闭链接
        close(pstmt, conn);

        return res;
    }

 


}

  

4.使用方法:

    @GetMapping("/api/testdruid")
    public String testdruid()throws SQLException {
        String sql = " update department set deptname=ltrim(rtrim(deptname)) ";
        return "ActiveCount = "+JDBCUtils.execSql(sql);
    }

  

 

posted on 2020-07-08 19:36  lpq22221314  阅读(503)  评论(0编辑  收藏  举报

导航