1.連接池概述
用池來管理Connection,這樣可以重復使用Connection,有了池,所以我們就不用自己來創建Connection,
而是通過池來獲取Connection對象,當使用完Connection后,調用Connection的close()方法也不會真的關閉Connection,
而是把Connection歸還給池,池就可以再利用這個Connection對象了
2.C3P0
public class Demo1 { @Test public void test() throws Exception { //獲得連接池 ComboPooledDataSource dataSource = new ComboPooledDataSource(); //設置連接池與數據庫的基本項 dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql:///demo"); dataSource.setUser("root"); dataSource.setPassword("root"); //初始化連接數目 dataSource.setInitialPoolSize(10); //最小連接個數 dataSource.setMinPoolSize(2); //最大連接個數 dataSource.setMaxPoolSize(20); //獲得連接 Connection connection = dataSource.getConnection(); System.out.println(connection); } }
3.常見的配置項
屬性 描述
user 用戶名
password 密碼
driverClass 驅動(mysql驅動:com.mysql.jdbc.Driver)
jdbcUrl jdbc:mysql:///數據庫
acquireIncrement:連接池無空閑連接可用時,一次性創建的新連接數 默認3
initialPoolSize :連接池初始化時創建的連接數 默認3
maxPoolSize :連接池中擁有的最大的連接數
minPoolSize :連接池保持的最小的連接數
4.c3p0配置文件使用
配置文件名字:c3p0-config.xml
配置文件位置:src
配置文件內容:命名配置
<c3p0-config> <!-- 命名的配置 --> <named-config name="itheima"> <!-- 連接數據庫的4項基本參數 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///demo</property> <property name="user">root</property> <property name="password">root</property> <!-- 如果池中數據連接不夠時一次增長多少個 --> <property name="acquireIncrement">5</property> <!-- 初始化連接數 --> <property name="initialPoolSize">20</property> <!-- 最小連接受 --> <property name="minPoolSize">10</property> <!-- 最大連接數 --> <property name="maxPoolSize">40</property> <!-- -JDBC的標准參數,用以控制數據源內加載的PreparedStatements數量 --> <property name="maxStatements">0</property> <!-- 連接池內單個連接所擁有的最大緩存statements數 --> <property name="maxStatementsPerConnection">5</property> </named-config> </c3p0-config>
5.c3p0工具類
public class Demo2 { //連接池 private static ComboPooledDataSource dataSource = new ComboPooledDataSource("datebase"); //獲得數據源 public static DataSource getDataSource() { return dataSource; } //獲得連接 public static Connection getConnection () throws SQLException { return dataSource.getConnection(); } }
6.dbcp連接池
常見屬性配置項
屬性 描述
username 用戶名
password 密碼
driverClassName 驅動(mysql驅動:com.mysql.jdbc.Driver)
url jdbc:mysql:///數據庫
maxActive :連接池中擁有的最大的連接數
minldle :最小空閑連接
maxldle :最大空閑連接
initialSize :初始化連接
public class Demo3 { @Test public void test() throws SQLException { //獲得連接池 BasicDataSource dataSource = new BasicDataSource(); //設置基本項 dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql:///demo"); dataSource.setUsername("root"); dataSource.setPassword("root"); // * 初始化連接池中連個的個數 dataSource.setInitialSize(5); // * 最大活動數 dataSource.setMaxActive(10); //2獲得連接 Connection conn = dataSource.getConnection(); } }
7.dbcp連接池配置文件使用
配置文件名稱:*.properties
配置文件位置:建議src
#連接設置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/webdb_4
username=root
password=root
initialSize=10
maxActive=50
maxIdle=20
minIdle=5
8.dbcp工具類
public class Demo4 { //創建連接池 private static DataSource dataSource; //加載配置文件,創建連接池 static{ try { InputStream is = Demo4.class.getClassLoader().getResourceAsStream("dbcp.properties"); Properties pro = new Properties(); pro.load(is); dataSource = BasicDataSourceFactory.createDataSource(pro); } catch (Exception e) { e.printStackTrace(); } } //獲得連接池 public static DataSource getDataSource() { return dataSource; } //獲得數據庫 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } }
9.DBUtils工具類
它是java編程中的數據庫操作實用工具
封裝了JDBC的操作,簡化了JDBC
三個核心功能:
QueryRunner中提供對sql語句操作的API
ResultHandler接口,用於定義select操作后,怎樣封裝結果集
DbUtils類,定義了關閉資源與實務處理的方法
QueryRunner核心類介紹
QueryRunner(DataSource)創建核心類,並提供數據源,內部自己維護connection
update(String sql,Object...params)執行DML語句
query(String sql,ResultSetHandler,Object..params)執行DQL語句,並將查詢結果封裝到對象中
10.QueryRunner結果處理類
ArrayHandler 將結果集中的第一條記錄封裝到一個Object[]數組中,數組中的每一個元素就是這條記錄中的每一個字段的值
ArrayListHandler 將結果集中的每一條記錄都封裝到一個Object[]數組中,將這些數組在封裝到List集合中。
BeanHandler 將結果集中第一條記錄封裝到一個指定的javaBean中。
BeanListHandler 將結果集中每一條記錄封裝到指定的javaBean中,將這些javaBean在封裝到List集合中
ColumnListHandler 將結果集中指定的列的字段值,封裝到一個List集合中
KeyedHandler 將結果集中每一條記錄封裝到Map<String,Object>,在將這個map集合做為另一個Map的value,另一個Map集合的key是指定的字段的值。
MapHandler 將結果集中第一條記錄封裝到了Map<String,Object>集合中,key就是字段名稱,value就是字段值
MapListHandler 將結果集中每一條記錄封裝到了Map<String,Object>集合中,key就是字段名稱,value就是字段值,在將這些Map封裝到List集合中。
ScalarHandler 它是用於單數據。例如select count(*) from 表操作。
11.JavaBean
1. 需要實現接口:java.io.Serializable ,通常實現接口這步驟省略了,不會影響程序。
2. 提供私有字段:private 類型 字段名;
3. 提供getter/setter方法:
4. 提供無參構造
特別補充:
ScalarHandler
/* * 查詢數據表結果集處理其中一種方式: * ScalarHandler處理方式 * 處理單值查詢結果,執行的select語句后,結果集只有1個 */ @Test public void demo01() throws SQLException{ // ScalarHandler : 用於處理聚合函數執行結果(一行一列) // * 查詢總記錄數 QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select count(*) from product"; Long obj = queryRunner.query(sql, new ScalarHandler<Long>()); //System.out.println(obj.getClass()); System.out.println(obj); }
MapHandler
/* * 查詢數據表結果集處理其中一種方式: * MapHandler處理方式 * 將數據表結果集的第一行數據,封裝成Map集合 * 鍵: 數據表中的列 * 值: 這個列中的數據 * * 處理方式的Map集合,是LinkedHashMap的子類 */ @Test public void demo02() throws SQLException{ // MapHandler : 將查詢到的一條記錄,封裝到Map中,map.key=字段名,map.value=值 // * 主要用途:多表操作、將數據轉換json 等 QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product where id = ?"; Object[] params = {6}; Map<String,Object> map = queryRunner.query(sql, new MapHandler(), params); System.out.println(map); // 將Map數據封裝到指定JavaBean }
MapListHandler
/* * 查詢數據表結果集其中一種處理方式: * MapListHandler處理方式 * 將數據表的結果集的每一行封裝成Map集合 * 數據表多行數據,出現多個Map集合,存儲List集合 */ @Test public void demo03() throws SQLException{ // MapListHandler : 查詢所有數據,將每一條記錄封裝到Map中,然后將Map添加到List中,最后返回List // * 主要用途:多表操作 等 QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product"; List<Map<String,Object>> list = queryRunner.query(sql, new MapListHandler()); for(Map<String,Object> map : list){ System.out.println(map); } }
KeyHandler
@Test public void demo04() throws SQLException{ // KeyedHandler : new KeyedHandler("字段名稱"),查詢所有,將查詢結果封裝到Map中 // * map.key=為指定“字段名稱”對應的值 // * map.value=為當前整條記錄所有的值,數據為Map<字段名,值> // 類型 Map<String , Map<String,Object> > QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product"; Map<String,Map<String,Object>> map = queryRunner.query(sql, new KeyedHandler<String>("name")); for(Map.Entry<String, Map<String,Object>> entry : map.entrySet()){ System.out.println(entry.getKey()); System.out.println(entry.getValue()); } }
ColumnListHandler
/* * 查詢數據表結果集處理其中一種方式: * ColumnListHandler處理方式 * 將查詢數據表結果集中的某一列數據,存儲到List集合 * 哪個列不清楚,數據類型也不清楚, List<Object> * ColumnListHandler構造方法 * 空參數: 獲取就是數據表的第一列 * int參數: 傳遞列的順序編號 * String參數: 傳遞列名 * * 創建對象,可以加入泛型,但是加入的數據類型,要和查詢的列類型一致 */ @Test public void demo05() throws SQLException{ // ColumnListHandler : 查詢指定一列數據 QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource()); String sql = "select * from product"; List<String> list = queryRunner.query(sql, new ColumnListHandler<String>("name")); System.out.println(list); }