栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据库连接池

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据库连接池

Druid
  • Druid是阿里巴巴开源连接池组件,是最好的连接池之一

  • Druid对数据库连接进行有效管理与重用,最大化程序执行效率

  • 连接池负责创建管理连接程序只负责取用与归还

 

Druid连接池的配置与使用

引入依赖


      com.alibaba
      druid
      1.1.12

创建属性文件

druid-config.properties

# 配置数据库的连接参数
driverClassName= com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/servicecenter?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true
username=root
password=root

# 配置连接池的参数
initialSize=5    //连接池准备好之后会默认地创建5个数据库连接
maxActive=10     //若5个链接不够用,那么由连接池创建新的连接,但是总数不超过10
maxWait=3000
maxIdle=6
minIdle=3

创建类

DruidSample

public class DruidSample {
    public static void main(String[] args) {

        //1.加载属性文件
        Properties properties = new Properties();
        String propertyFile = DruidSample.class.getResource("druid-config.properties").getPath();
        //路径中  空格->%20    C: java codedruid-config.properties
        //c:java%20codedruid-config.properties

        try {
            propertyFile = new URLDecoder().decode(propertyFile,"UTF-8");
        } catch (UnsupportedEncodingException e) {  //捕捉不支持的编码异常
            e.printStackTrace();
        }
        try {
            properties.load(new FileInputStream(propertyFile));
        } catch (Exception e) {
            e.printStackTrace();
        }

        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            //2.获取DataSource数据源对象
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
            //3.创建数据库连接
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("selete * from employee limit 0,10");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Integer empId = rs.getInt(1) ;
                String ename = rs.getString ("ename") ;
                String dname = rs.getString ("dname") ;
                Float salary = rs.getFloat ("salary") ;
                System.out.println(dname + "-" + empId + "-" + ename + "-" + salary);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DbUtils.closeConnection(rs,pstmt,conn);
        }
    }
}
C3P0连接池

c3p0-config.xml



	
        com.mysql.cj.jdbc.Driver
        jdbc:mysql://localhost:3306/servicecenter?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=true
         root
        root
        
        10
        
        20
	

C3P0Sample

public class C3P0Sample {
    public static void main(String[] args) {

        //1.加载属性文件
        //2.获取DataSource数据源对象
        DataSource dataSource = ComboPooledDataSource();
        //3.创建数据库连接
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try{
            conn = dataSource.getConnection();
            pstmt = conn.prepareStatement("selete * from employee limit 0,10");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Integer empId = rs.getInt(1) ;
                String ename = rs.getString ("ename") ;
                String dname = rs.getString ("dname") ;
                Float salary = rs.getFloat ("salary") ;
                System.out.println(dname + "-" + empId + "-" + ename + "-" + salary);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            DbUtils.closeConnection(rs,pstmt,conn);
        }
    }
}

暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1017251.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号