zoukankan      html  css  js  c++  java
  • DbUtil组件及C3P0数据库连接池组件的使用

    DbUtils

    Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能。

    使用commons-dbutils 的核心工具类:QueryRunner,该类定义了所有操作数据库的方法

    如方法:T query(Connection conn ,String sql, ResultSetHandler<T> rsh, Object... params)

    DbUtils提供的封装结果的一些对象:

    1) BeanHandler  查询返回单个对象(常用)

    2) BeanListHandler  查询返回list集合,集合元素是指定的对象(常用

    3)  ArrayHandler  查询返回结果记录的第一行,封装对对象数组, 即返回:Object[]

    4)  ArrayListHandler 把查询的每一行都封装为对象数组,再添加到list集合中

    5)  ScalarHandler 查询返回结果记录的第一行的第一列  (在聚合函数统计的时候用)

    6)  MapHandler  查询返回结果的第一条记录封装为map

    使用:

    前提:实体类必须符合javabean规范,并且实体类中的字段必须与数据库表的字段相同。引入jar文件 : commons-dbutils-1.6.jar

    1、简单创建一个工具类JdbcUtil,方便代码调用

     1 public class JdbcUtil {
     2     private static String url = "jdbc:mysql:///test01";
     3     private static String user = "root";
     4     private static String password = "123456";
     5     //获取QueryRunner对象
     6     public static QueryRunner getQueryRunner(){
     7         return new QueryRunner();
     8     }
     9     //获取连接
    10     public static Connection getConnection(){
    11         try {
    12             Class.forName("com.mysql.jdbc.Driver");
    13             return DriverManager.getConnection(url, user, password);
    14         } catch (Exception e) {
    15             e.printStackTrace();
    16             throw new RuntimeException(e);
    17         }
    18     }
    19 }

     2、调用query方法

     1 @Test
     2     public void test1() {
     3         List<Student> list = null;
     4         try {
     5             Connection conn = JdbcUtil.getConnection();
     6             list = JdbcUtil.getQueryRunner().query(conn, "select * from Student"
     7                     , new BeanListHandler<Student>(Student.class));
     8         } catch (Exception e) {
     9             e.printStackTrace();
    10             throw new RuntimeException(e);
    11         }
    12         if(list !=null){
    13             for (Student student : list) {
    14                 System.out.println(student);
    15             }
    16         }
    17     } 

    使用C3P0数据库连接池组件优化程序性能

    C3P0核心类:ComboPooledDataSource

    使用:

    前提 引入jar文件 : c3p0-0.9.1.2.jar
    方式一:不使用配置文件

    1、简单建立一个jdbc工具类,方便方法调用

     1 import java.beans.PropertyVetoException;
     2 import java.sql.Connection;
     3 import org.apache.commons.dbutils.QueryRunner;
     4 
     5 import com.mchange.v2.c3p0.ComboPooledDataSource;
     6 
     7 
     8 public class JdbcUtil {
     9     private static String url = "jdbc:mysql:///test01";
    10     private static String user = "root";
    11     private static String password = "123456";
    12     private static ComboPooledDataSource dataSource = null;
    13     private Connection con = null;
    14     static{
    15         //初始化操作
    16         dataSource = new ComboPooledDataSource();// 使用默认的配置
    17         dataSource.setJdbcUrl(url);//设置连接字符串
    18         try {
    19             dataSource.setDriverClass("com.mysql.jdbc.Driver");//获取驱动
    20         } catch (PropertyVetoException e) {
    21             e.printStackTrace();
    22         }
    23         dataSource.setUser(user);//用户名
    24         dataSource.setPassword(password);//密码
    25         dataSource.setInitialPoolSize(3);//初始化时获取三个连接
    26         dataSource.setMaxPoolSize(6);//连接池中保留的最大连接数
    27         dataSource.setMaxIdleTime(60); //最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃
    28     }
    29     
    30     //获取QueryRunner对象
    31     public static QueryRunner getQueryRunner(){
    32         return new QueryRunner(dataSource);
    33     }
    34     //获取连接纯 通过c3p0核心类对象获取(此例子没用到该方法)
    35     public static Connection getConnection(){
    36         try {
    37             return dataSource.getConnection();
    38         } catch (Exception e) {
    39             e.printStackTrace();
    40             throw new RuntimeException(e);
    41         }
    42     }
    43 }

    2、执行测试

     1 @Test
     2     public void test1() {
     3         List<Student> list = null;
     4         try {
     5             Connection conn = JdbcUtil.getConnection();
     6             list = JdbcUtil.getQueryRunner().query("select * from Student"
     7                     , new BeanListHandler<Student>(Student.class));
     8         } catch (Exception e) {
     9             e.printStackTrace();
    10             throw new RuntimeException(e);
    11         }
    12         if(list !=null){
    13             for (Student student : list) {
    14                 System.out.println(student);
    15             }
    16         }
    17     }

    方式二:使用配置文件来初始化

    1、将C3P0配置文件c3p0-config.xml放置在工程src目录下

    c3p0-config.xml

     1 <c3p0-config>
     2   <!-- 默认加载配置 -->
     3   <default-config>
     4      <property name="driverClass">com.mysql.jdbc.Driver</property> 
     5      <property name="jdbcUrl">jdbc:mysql://localhost:3306/test01</property> 
     6      <property name="user">root</property> 
     7      <property name="password">123456</property> 
     8      <property name="initialPoolSize">5</property> 
     9      <property name="maxPoolSize">10</property> 
    10   </default-config>
    11   <!-- 指定名称加载配置 -->
    12   <named-config name="C3P0TestName">
    13     <property name="driverClass">com.mysql.jdbc.Driver</property> 
    14      <property name="jdbcUrl">jdbc:mysql://localhost:3306/test01</property> 
    15      <property name="user">root</property> 
    16      <property name="password">123456</property> 
    17      <property name="initialPoolSize">5</property> 
    18      <property name="maxPoolSize">10</property> 
    19   </named-config>
    20   
    21 </c3p0-config>

    2、简单编写一个工具类,方便代码调用

     1 import java.sql.Connection;
     2 import org.apache.commons.dbutils.QueryRunner;
     3 
     4 import com.mchange.v2.c3p0.ComboPooledDataSource;
     5 
     6 
     7 public class JdbcUtil {
     8     private static ComboPooledDataSource dataSource = null;
     9     static{
    10         //初始化操作
    11         // 自动加载src目录下c3p0的配置文件【c3p0-config.xml】
    12         dataSource = new ComboPooledDataSource();// 使用默认的配置
    13         //使用c3p0-config.xml配置文件中named-config的name属性为C3P0TestName的配置
    14         //dataSource = new ComboPooledDataSource("C3P0TestName");
    15     }
    16     
    17     //获取QueryRunner对象
    18     public static QueryRunner getQueryRunner(){
    19         return new QueryRunner(dataSource);
    20     }
    21     //获取连接纯 通过c3p0核心类对象获取(此例子没用到该方法)
    22     public static Connection getConnection(){
    23         try {
    24             return dataSource.getConnection();
    25         } catch (Exception e) {
    26             e.printStackTrace();
    27             throw new RuntimeException(e);
    28         }
    29     }
    30 }

    3、执行测试

     1 @Test
     2     public void test1() {
     3         List<Student> list = null;
     4         try {
     5             Connection conn = JdbcUtil.getConnection();
     6             list = JdbcUtil.getQueryRunner().query("select * from Student"
     7                     , new BeanListHandler<Student>(Student.class));
     8         } catch (Exception e) {
     9             e.printStackTrace();
    10             throw new RuntimeException(e);
    11         }
    12         if(list !=null){
    13             for (Student student : list) {
    14                 System.out.println(student);
    15             }
    16         }
    17     }

    完毕.

  • 相关阅读:
    TopCoder<SRM>上的一道1100分的题目解析附代码
    《算法导论》思考题15-2 整齐打印
    《算法导论》思考题15-1 双调欧几里得旅行商问题(动态规划)
    <ReversingEngineering>关于windows32位系统下的dll注入技术经验汇
    实现一个EventEmitter类,这个类包含以下方法: on/ once/fire/off
    [Jquery 插件]活动倒计时,可同步服务器时间,倒计时格式随意设置
    阻止a标签跳转四种方法 兼容各大浏览器(包括IE)
    git常用操作 配置用户信息、拉取项目、提交代码、分支操作、版本回退...
    nrm npm源管理利器
    Element UI 框架搭建
  • 原文地址:https://www.cnblogs.com/fnz0/p/5858546.html
Copyright © 2011-2022 走看看