在jdbc基礎上進階一小步的C3p0 串連池(DBCP 不能讀xml設定檔,已淘汰) 和DBUtils 中兩個主要類QueryRunner和ResultSetHandler的使用,
首先看C3p0這個串連池,最大優勢可以自動讀取預設的設定檔
<?xml version="1.0" encoding="UTF-8"?><c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/swift_db </property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">20</property> <property name="acquireIncrement">5</property> <property name="maxPoolSize">50</property> <property name="minPoolSize">5</property> </default-config></c3p0-config>
設定檔中有常規的4個主選項和一些其他配置
只需要使用C3p0中的實現javax.sql.DateSource介面的實作類別ComboPooledDataSource 建立對象即可
private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
這個是放成員變數那,只要new 空參就會自動調用xml設定檔,設定檔需要標籤<default-config>
也可以調用命名的設定檔,需要設定檔<named-config>
<?xml version="1.0" encoding="UTF-8"?><c3p0-config> <named-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/swift_db </property> <property name="user">root</property> <property name="password">root</property> <property name="initialPoolSize">20</property> <property name="acquireIncrement">5</property> <property name="maxPoolSize">50</property> <property name="minPoolSize">5</property> </named-config></c3p0-config>
一般把C3p0做成工具類使用,方便調用
package com.swift.base;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class C3p0Utils { // 資料來源串連池c3p0作用不用每次建立串連,浪費資源與等待時間,把串連做好放在串連池中,需要時擷取 // c3p0串連池可以通過設定檔c3p0-config.xml檔案自動讀取相關屬性 // c3p0串連池是java中 javax.sql.DataSource的實作類別,其中必須實現getConnection方法 private static ComboPooledDataSource dataSource = new ComboPooledDataSource("swift"); // 獲得資料來源(串連池)的方法 public static DataSource getDataSource() { return dataSource; } // 從串連池中獲得一個conn資源的 public static Connection getConnection() throws Exception { dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/swift_db"); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setAcquireIncrement(5); dataSource.setInitialPoolSize(20); dataSource.setMinPoolSize(5); dataSource.setMaxPoolSize(50); Connection conn = dataSource.getConnection(); return conn; } public static void close(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }}
這個已經調用了xml設定檔,每次又調用Connection都會來一遍屬性配置,浪費資源,可以用靜態代碼塊,只調用一次即可
package com.swift.jinji;import java.beans.PropertyVetoException;import java.sql.Connection;import java.sql.SQLException;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;/*使用C3P0獲得10個連線物件要求:不使用設定檔*/public class C3p0_10con { private static ComboPooledDataSource dataSource = new ComboPooledDataSource(); static { try { dataSource.setDriverClass("com.mysql.jdbc.Driver"); } catch (PropertyVetoException e) { e.printStackTrace(); } dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/swift_db"); dataSource.setUser("root"); dataSource.setPassword("root"); dataSource.setInitialPoolSize(20); dataSource.setAcquireIncrement(5); dataSource.setMaxPoolSize(50); dataSource.setMinPoolSize(5); } public static void main(String[] args) throws PropertyVetoException { for(int i=0;i<10;i++) { System.out.println(C3p0_10con.getConnection()); } } public static DataSource getDataSource() { return dataSource; } public static Connection getConnection() { Connection con = null; try { con = dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return con; } public static void close(Connection con) { if(con!=null) { try { con.close(); } catch (SQLException e) { e.printStackTrace(); } } }}
配合DBUtils的QureyRunner類和ResultSetHandler類使用,更加方便資料庫增刪改與查詢
package com.swift.jinji;import java.sql.SQLException;import java.util.Arrays;import java.util.List;import java.util.Map;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.ResultSetHandler;import org.apache.commons.dbutils.handlers.ArrayHandler;import org.apache.commons.dbutils.handlers.ArrayListHandler;import org.apache.commons.dbutils.handlers.BeanHandler;import org.apache.commons.dbutils.handlers.ColumnListHandler;import org.apache.commons.dbutils.handlers.KeyedHandler;import org.apache.commons.dbutils.handlers.MapHandler;import org.apache.commons.dbutils.handlers.MapListHandler;import org.apache.commons.dbutils.handlers.ScalarHandler;import com.swift.base.C3p0Utils;import com.swift.domain.Puppy;/*查詢使用者表中的第一條資料。並將資料封裝成對象數組。*/public class Qurey_Puppy { public static void main(String[] args) { QueryRunner qr=new QueryRunner(C3p0Utils.getDataSource()); String sql="select * from puppy;"; ResultSetHandler<Puppy> rsh=new BeanHandler<Puppy>(Puppy.class); ResultSetHandler<Object[]> rsh1=new ArrayHandler(); ResultSetHandler<List<Object[]>> rsh2=new ArrayListHandler(); ResultSetHandler<Object> rsh3=new ScalarHandler("name"); ResultSetHandler<Map<String, Object>> rsh4=new MapHandler(); ResultSetHandler<List<Map<String, Object>>> rsh5=new MapListHandler(); ResultSetHandler<List<Object>> rsh6=new ColumnListHandler("name"); ResultSetHandler<Map<Object,Map<String,Object>>> rsh7=new KeyedHandler("id"); try { Map<Object, Map<String, Object>> result = qr.query(sql, rsh7); /* for(Object[] puppy:dog) { System.out.println(Arrays.toString(puppy)); }*/// System.out.println(result); } catch (SQLException e) { e.printStackTrace(); } }}
ResultSetHandler有很多子類,可以放到各種容器中,從資料庫取出資料後,將資料放到對象數組,數組集合,對象元素,Map集合,Map集合列表,對象列表,Map的Map中