天天看點

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     }      

完畢.

下一篇: servlet過濾器

繼續閱讀