JDBC总结

1.c3p0是一个开放源代码的JDBC连接池,c3p0有自动回收空闲连接功能。c3p0提供了最大的空闲时间,当连接超过最大空闲连接时间时,当前连接就会被断掉。c3p0所应用的类是:ComboPooledDataSource

加入 c3p0 jar包,c3p0通过 c3p0.config.xml 文件进行配置;将 c3p0 提供的 c3p0.config.xml 拷贝到src目录下,该文件指定了连接数据库和连接池的相关参数。

<c3p0-config>
       <!-- 数据库名称代表连接池 -->
    <named-config name ="mysql">
<!--  驱动类 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <!--  url -->
      <property name="jdbcUrl">jdbc:mysql://localhost:3306/fu_db03</property>
    <!--  用户名 -->
           <property name="user">root</property>
           <!--  密码 -->
      <property name="password">root</property>
      <!--  每次增长的连接数 -->
      <property name="acquireIncrement">5</property>
      <!--  初始的连接数 -->
      <property name="initialPoolSize">10</property>
      <!--  最小连接数 -->
      <property name="minPoolSize">5</property>
      <!--  最大连接数 -->
      <property name="maxPoolSize">50</property>

      <!--  可连接的最多的命令对象 -->
      <property name="maxStatements">5</property>

      <!--  每个连接对象可连接的最多的命令对象数 -->
      <property name="maxStatementsPerConnection">2</property>
    </named-config>
</c3p0-config>
public static void main(String[] args) throws Exception {
    ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("mysql");
    long start = System.currentTimeMillis();
    System.out.println("开始执行...");
    for (int i = 0; i < 500000; i++) {
        Connection connection = comboPooledDataSource.getConnection();
        connection.close();
    }
    long end = System.currentTimeMillis();
    System.out.println("c3p0连接池 操作500000 耗时=" + (end - start));
}

2.Druid是阿里开源的数据库连接池,性能比dbcp、c3p0更高,使用也越来越广泛。

加入 Druid jar包;加入配置文件 druid.properties ,将文件拷贝到项目的src目录;创建Properties对象,读取配置文件;创建一个指定参数的数据库Druid连接池。

#key=value
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/fu_db03?rewriteBatchedStatements=true
username=root
password=root
#initial connection Size
initialSize=10
#min idle connection size
minIdle=5
#man active connection size
maxActive=50
#max wait time (5000 mil seconds)
maxWait=3000
public static void main(String[] args) throws Exception {
    Properties properties = new Properties();
    properties.load(new FileInputStream("src\\druid.properties"));
    DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
    long start = System.currentTimeMillis();
    for (int i = 0; i < 500000; i++) {
        Connection connection = dataSource.getConnection();
        connection.close();
    }
    long end = System.currentTimeMillis();
    System.out.println("druid连接池 操作500000 耗时=" + (end - start));
}

3.commons-dbutils是Apache组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装。DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。DbUtils :提供如关闭连接、装载JDBC驱动程序等工具类,里面的所有方法都是静态的。DbUtils常用API:DbUtils类、QueryRunner类、ResultSetHandler接口。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值