zoukankan      html  css  js  c++  java
  • 连接池的基本语法

    protected Connection con;
    protected PreparedStatement ps;
    protected ResultSet re;
    /**连接池对象*/
    private static BasicDataSource bds=new BasicDataSource();
    static{
      //设置驱动
      bds.setDriverClassName("org.gjt.mm.mysql.Driver");
      //设置连接URL
      bds.setUrl("jdbc:mysql://localhost:3306/myitem?characterEncoding=utf-8");
      //设置数据库登录用户名
      bds.setUsername("root");
      //设置数据库登录密码
      bds.setPassword("1234");
      //设置最大连接数
      bds.setMaxActive(300);
      //设置最少连接数
      bds.setMaxIdle(100);
      //设置最大等待时间
      bds.setMaxWait(1000);
      }
    public void setConnection(){
      try {
      //从连接池中取出一个链接对象
        this.con=bds.getConnection();

      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    /**
    * 关闭连接,将链接对象的状态从忙碌变为空闲
    */
    public void closeConnection(){
      try {
        if(re!=null){
          re.close();
        }
        if(ps!=null){
          ps.close();
        }
        if(con!=null){
          con.close();
        }

      } catch (SQLException e) {
        e.printStackTrace();
      }
    }

  • 相关阅读:
    发夹模式的使用及应用场景
    springBoot项目配置日志打印管理(log4j2)
    idea创建springBoot项目
    修改jdk注册表
    文件下载——下载Excel
    stream().filter()的用法
    文件上传——Spring MVC跨服务器
    文件上传——Spring MVC方式
    文件上传——传统方式
    Spring MVC响应数据方式
  • 原文地址:https://www.cnblogs.com/cj28-27/p/5492132.html
Copyright © 2011-2022 走看看