java---数据库连接池

一、数据库连接池

  1、什么是数据库连接池?

   池,就是一类东西同时大量存在的地方,例如缓存池。数据库连接池就是存放数据库连接的地方。

  2、为什么要使用数据库连接池?

   在我们连接数据库时,要加载数据库驱动、创建连接,如果不使用数据库连接池,那么每连接一次数据库,就要创建一次连接,使用完了就销毁。如果连接量很少,没有问题,但是如果瞬间有超多的访问,内存和数据库都保不住。

  3、数据库连接池的功能介绍

    数据库连接池可以对数据库连接分配、管理和销毁,允许应用程序可以重复的使用一个已经的存在的数据库连接,而不是每使用一次就要创建一次。数据库连接池能够设置最大同时存在的数据库连接数,数据库连接的最大存活时间。

    **使用数据库连接池后,就不用再手动创建数据库连接,数据库连接的创建交给数据库连接池。

  4、数据库连接池的种类

    ①c3p0

    ②DBpool

    ③Proxool

    ④Druid(德鲁伊特):阿里开发的数据库连接池,具有以上所有数据库连接池的优点,学这个

  5、数据库连接的使用

    ①导入jar包,可以到maven官网下载druid-1.1.9.jar

    ②写配置文件 db.properties

      DriverClassName="数据库驱动"

      url="数据库连接"

      username="用户名"

      password="密码"

    ③改造DBUtils

      关键语句:   DataSource datasource=DruidDataSourceFactory.createDataSource(配置文件流);  //创建数据库连接池

            datasource.getConnection(); //获取数据库连接

      具体代码:

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

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

/**
 * @Description: 工具类
 * @ClassName: JDBCUtil
 * @Author: fengyun
 * @Date: 2020/12/30 10:41
 * @Version: 1.0
 */
public class JDBCUtil {
    private static DataSource dataSource=null;
    /*
    * 使用阿里连接池改造代码,如果不使用连接池,需要我们自己获取读取到的配置文件的参数,然后手动加载驱动,创建连接。
    * 使用连接池后,durid可以自己识别配置文件的参数,自己加载驱动,创建连接
    * */
    static {
        try {
            InputStream raStream = JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties");
            Properties properties = new Properties();
            properties.load(raStream);

            dataSource = DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConn(){
        try {
            Connection connection = dataSource.getConnection();
            return connection;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    public static void setP(PreparedStatement ps,Object... args){
        try{
            if(args.length>0){
                for (int i = 0; i < args.length; i++) {
                    ps.setObject(i+1,args[i]);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void close(ResultSet rs,PreparedStatement ps,Connection conn){
        try {
            if(rs!=null){
                rs.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            if(ps!=null){
                ps.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        try {
            if(conn!=null){
                conn.close();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    public static List<List>  queryList(String sql,Object... args){
        Connection conn = getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        ArrayList<List> bigList=null;
        try {
            ps = conn.prepareStatement(sql);
            setP(ps,args);

            rs = ps.executeQuery();
            bigList = new ArrayList<>();
            while (rs.next()){
                ArrayList<Object> sList = new ArrayList<>();
                for (int i = 1; i <=rs.getMetaData().getColumnCount() ; i++) {
                    sList.add(rs.getObject(i));
                }
                bigList.add(sList);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(rs,ps,conn);
        }
        return bigList;
    }

    public static List<Map>  queryMapList(String sql, Object... args){
        Connection conn = getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        ArrayList<Map> bigList=null;
        try {
            ps = conn.prepareStatement(sql);
            setP(ps,args);

            rs = ps.executeQuery();
            bigList = new ArrayList<>();
            while (rs.next()){
                HashMap<String, Object> hm = new HashMap<>();
                for (int i = 1; i <=rs.getMetaData().getColumnCount() ; i++) {
                    hm.put(rs.getMetaData().getColumnName(i),rs.getObject(i));
                }
                bigList.add(hm);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(rs,ps,conn);
        }
        return bigList;
    }

    public static int updateSql(String sql,Object... args){
        Connection conn = getConn();
        PreparedStatement ps=null;
        try {
            ps = conn.prepareStatement(sql);
            setP(ps,args);

            int i = ps.executeUpdate();
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(null,ps,conn);
        }
        return -1;
    }

    public static int  getPk(String sql,Object... args){
        Connection conn = getConn();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
            setP(ps,args);
            int i = ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            if(rs!=null){
                int anInt = rs.getInt(1);
                return anInt;
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(rs,ps,conn);
        }
        return -1;
    }
}

 

posted @ 2021-01-12 09:19  橙汁one  阅读(594)  评论(0编辑  收藏  举报