数据库连接池

Druid

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

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

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

 

Druid连接池的配置与使用

引入依赖

<dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.12</version>
</dependency>

创建属性文件

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 code\druid-config.properties
        //c:\java%20code\druid-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

<?xml version= ="1.0" encoding ="UTF-8" ?>
<c3p0-config>
	<default- config>
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/servicecenter?serverTimezone=Asia/Shanghai&amp;useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=true
        <property name="user"> root</property>
        <property name="password">root</property>
        <!-- 连接池初始连接数量-->
        <property name="initialPoolsize">10</property>
        <!--最大连接数量-->
        <property name="maxPoolsize">20</property>
	</default-config>
</c3p0-config>

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值