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);

        <span style="color: rgba(255, 0, 0, 1)">dataSource </span></span><span style="color: rgba(255, 0, 0, 1)">=</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(255, 0, 0, 1)"> DruidDataSourceFactory.createDataSource(properties);</span>
      

      } 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();
    }

     </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
         </span><span style="color: rgba(0, 0, 255, 1)">if</span>(ps!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
             ps.close();
         }
     } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException throwables) {
         throwables.printStackTrace();
     }
    
     </span><span style="color: rgba(0, 0, 255, 1)">try</span><span style="color: rgba(0, 0, 0, 1)"> {
         </span><span style="color: rgba(0, 0, 255, 1)">if</span>(conn!=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">){
             conn.close();
         }
     } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (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 </span>=<span style="color: rgba(0, 0, 0, 1)"> ps.executeQuery();
         bigList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
         </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (rs.next()){
             ArrayList</span>&lt;Object&gt; sList = <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
             </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 1; i &lt;=rs.getMetaData().getColumnCount() ; i++<span style="color: rgba(0, 0, 0, 1)">) {
                 sList.add(rs.getObject(i));
             }
             bigList.add(sList);
         }
     } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException throwables) {
         throwables.printStackTrace();
     }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
         close(rs,ps,conn);
     }
     </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> 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 </span>=<span style="color: rgba(0, 0, 0, 1)"> ps.executeQuery();
         bigList </span>= <span style="color: rgba(0, 0, 255, 1)">new</span> ArrayList&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
         </span><span style="color: rgba(0, 0, 255, 1)">while</span><span style="color: rgba(0, 0, 0, 1)"> (rs.next()){
             HashMap</span>&lt;String, Object&gt; hm = <span style="color: rgba(0, 0, 255, 1)">new</span> HashMap&lt;&gt;<span style="color: rgba(0, 0, 0, 1)">();
             </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 1; i &lt;=rs.getMetaData().getColumnCount() ; i++<span style="color: rgba(0, 0, 0, 1)">) {
                 hm.put(rs.getMetaData().getColumnName(i),rs.getObject(i));
             }
             bigList.add(hm);
         }
     } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException throwables) {
         throwables.printStackTrace();
     }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
         close(rs,ps,conn);
     }
     </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> bigList;
    

    }

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

         </span><span style="color: rgba(0, 0, 255, 1)">int</span> i =<span style="color: rgba(0, 0, 0, 1)"> ps.executeUpdate();
         </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> i;
     } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException throwables) {
         throwables.printStackTrace();
     }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
         close(</span><span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">,ps,conn);
     }
     </span><span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 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;
    }

     } </span><span style="color: rgba(0, 0, 255, 1)">catch</span><span style="color: rgba(0, 0, 0, 1)"> (SQLException throwables) {
         throwables.printStackTrace();
     }</span><span style="color: rgba(0, 0, 255, 1)">finally</span><span style="color: rgba(0, 0, 0, 1)"> {
         close(rs,ps,conn);
     }
     </span><span style="color: rgba(0, 0, 255, 1)">return</span> -1<span style="color: rgba(0, 0, 0, 1)">;
    

    }
    }