• 数据库连接池知识点总结-DX的笔记


    什么是数据库连接池?

    • 类似于线程池,创建一个集合,包含了多个数据库连接
    • 需要使用数据库链接时,不需要自己创建,直接向连接池要,用完再还给连接池
    • 连接池效率高,尤其是高并发的时候
    • 程序员不需要关心连接的创建和销毁

    常用的数据库连接池

    •  a. dbcp
      
      • 1
    •  b. c3p0
      
      • 1
    •  c. druid
       *      阿里的产品
       *      目前市场的主流
      
      • 1
      • 2
      • 3

    数据库连接池的使用方法

    创建配置文件

    • dbcp和druid需要创建一个xxx.properties的配置文件,里面写数据库的配置信息,注意不同数据库的命名规则不同

    • 创建配置文件:dbcp.properties

      driverClassName=com.mysql.cj.jdbc.Driver
      url=jdbc:mysql:///stusys
      username=root
      password=111111
      
      • 1
      • 2
      • 3
      • 4
    • druid.properties

      driverClassName=com.mysql.cj.jdbc.Driver
      url=jdbc:mysql:///stusys
      username=root
      password=111111
      
      • 1
      • 2
      • 3
      • 4
    • c3p0-config.xml c3p0使用xml文件

      <c3p0-config>
          <default-config>
              <property name="driverClass">com.mysql.cj.jdbc.Driverproperty>
              <property name="jdbcUrl">jdbc:mysql:///stusysproperty>
              <property name="user">rootproperty>
              <property name="password">111111property>
              
          default-config>
      c3p0-config>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

      写一个工具类

      工具类提供获取连接的方法,以后可以直接使用

      public class JDBCUtil3 {
      
          private static ThreadLocal<Connection> tl = new ThreadLocal<>();
          private static DataSource ds=null;
      
          static {
              InputStream is = null;
              try {
                  //从配置文件获取资源(c3p0不需要)
                  //1.读取资源
      //            is = JDBCUtil3.class.getClassLoader().getResourceAsStream("dbcp.properties");
                  is = JDBCUtil3.class.getClassLoader().getResourceAsStream("druid.properties");
                  //2.获取Properties对象
                  Properties properties = new Properties();
                  properties.load(is);
      
                  //dbcp
      //            ds = BasicDataSourceFactory.createDataSource(properties);
                  //c3p0
      //            ds = new ComboPooledDataSource();
                  //druid
                  ds = DruidDataSourceFactory.createDataSource(properties);
      
              } catch (Exception e) {
                  e.printStackTrace();
              }finally {
                  if (is != null) {
                      try {
                          is.close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
              }
          }
          //获取数据库连接
          public static Connection getConnection() throws Exception {
              Connection conn = tl.get();
              if (conn==null){
                  conn = ds.getConnection();
                  tl.set(conn);
              }
              return conn;
          }
          // 提供关闭资源的方法
          public static void close(Connection conn, Statement sta, ResultSet rs){
              if (rs != null){
                  try {
                      rs.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
              if (sta != null){
                  try {
                      sta.close();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
              if (conn != null){
                  try {
                      conn.close();
                      // 清除本地线程绑定的连接
                      tl.remove();
                  } catch (SQLException e) {
                      e.printStackTrace();
                  }
              }
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32
      • 33
      • 34
      • 35
      • 36
      • 37
      • 38
      • 39
      • 40
      • 41
      • 42
      • 43
      • 44
      • 45
      • 46
      • 47
      • 48
      • 49
      • 50
      • 51
      • 52
      • 53
      • 54
      • 55
      • 56
      • 57
      • 58
      • 59
      • 60
      • 61
      • 62
      • 63
      • 64
      • 65
      • 66
      • 67
      • 68
      • 69
      • 70
      • 71

      使用工具类

      • DAO层
      public int selectAdmin(String user) {
      
          Connection connection=null;
          PreparedStatement statement=null;
          ResultSet res=null;
          int count=0;
          try {
              //获取数据库连接对象
              connection= JDBCUtil3.getConnection();
              //写sql
              String sql="select count(id) from admin where username=?";
              //创建statement
              statement = connection.prepareStatement(sql);
              //添加参数
              statement.setString(1,user);
              //执行查询
              res=statement.executeQuery();
              //获取结果
              if (res.next()){
                  count=res.getInt(1);
              }
          } catch (Exception throwables) {
              throwables.printStackTrace();
          }finally {
              //关闭连接  service层可能使用事务,这里connection未关闭
              JDBCUtil3.close(null,statement,res);
          }
          System.out.println("查询到count="+count);
          return count;
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • service层(可以使用事务)

        public boolean register(String username, String password) {
        
            //手动提交事务
            Connection connection = null;
            int res=0;
            try {
                connection = JDBCUtil3.getConnection();
                connection.setAutoCommit(false);
                //先查询是否已存在
                res=new SelectDaoImpl().selectAdmin(username);
                if (res==1){
                    return false;
                }
                //添加用户
                int addres=new AdminDaoImpl().addUser(username, password);
                if (addres==0){
                    return false;
                }
                //提交事务
                connection.commit();
            } catch (Exception e) {
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
                e.printStackTrace();
            }finally {
                //关闭连接
                JDBCUtil3.close(connection,null,null);
            }
            return true;
        }
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
        • 21
        • 22
        • 23
        • 24
        • 25
        • 26
        • 27
        • 28
        • 29
        • 30
        • 31
        • 32
        • 33
  • 相关阅读:
    css 实现文字流光效果
    Rockland甘油激酶抗体生物素偶联物丨山羊多克隆
    英伟达RTX4090又火了?这次是真的着火了
    LNMP搭建
    Vue知识系列(3)每天10个小知识点
    Jupyter 介绍
    第42期:MySQL 是否有必要多列分区
    开放大学生活的新引领——电大搜题助力重庆开放大学学子实现梦想
    Unity获取脚本的CustomEditor(自定义编辑)数据
    【WP】猿人学13_入门级cookie
  • 原文地址:https://blog.csdn.net/qq_43528471/article/details/126591961