您好,登錄后才能下訂單哦!
package com.hanchao.test; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner; import javax.sql.DataSource; import com.mchange.v2.c3p0.DataSources; /*********************** * @author:han * @version:1.0 * @created:2015-10-11 *********************** */ public class TestPool { public static void main(String[] args) { /* Scanner input = new Scanner(System.in); System.out.println("請輸入賬號:"); String name = input.next(); System.out.println("請輸入密碼:"); String pwd = input.next(); System.out.println("請輸入金額:"); float money = input.nextFloat();*/ final String DRIVER = "com.mysql.jdbc.Driver"; //final String URL = "jdbc:mysql://127.0.0.1:3306/mydb"; //final String URL = "jdbc:mysql://localhost:3306/mydb"; final String URL = "jdbc:mysql:///mydb"; final String NAME = "root"; final String PASSWORD = "root"; Connection conn = null; Statement stat = null; ResultSet rs = null; try { //1.加載數據庫驅動 Class.forName(DRIVER); //連接mysql數據庫 DataSource unpooled = DataSources.unpooledDataSource(URL,NAME,PASSWORD); //構建一個連接池 DataSource pooled = DataSources.pooledDataSource(unpooled); //2.獲取數據庫連接(first Time) conn = pooled.getConnection(); System.out.println("1 con Class Type is :" + conn.getClass().getName()); //取得內部的實際數據庫連接 Object o1 = getInner(conn); System.out.println("1 Inner con Class Type is :" + o1.getClass().getName()); //3.獲取Statement對象 stat = conn.createStatement(); //4.執行SQL rs = stat.executeQuery(" select * from t_user where id = 13"); while (rs.next()) { System.out.println(" username:" + rs.getString("username")); } //5.關閉連接 rs.close(); stat.close(); conn.close(); //6.等待連接返回池中 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } //第二次獲取數據庫連接 conn = pooled.getConnection(); System.out.println("2 con Class Type is :" + conn.getClass().getName()); Object o2 = getInner(conn); System.out.println("2 Inner con Class Type is :" + o2.getClass().getName()); //獲取Statement對象 stat = conn.createStatement(); //3.獲取Statement對象 stat = conn.createStatement(); //4.執行SQL rs = stat.executeQuery(" select * from t_user where id = 13"); while (rs.next()) { System.out.println(" username:" + rs.getString("username")); } /* //3.獲取Statement對象 // String sql = "delete from t_user where id = 13"; // String sql = "insert into t_user(username,address) values('tom1','USA1')"; StringBuilder sql = new StringBuilder(); sql.append("INSERT INTO t_account(username,`password`,money,`enable`) "); sql.append("VALUES "); sql.append("('"+name+"','"+pwd+"','"+money+"','1')"); System.out.println("SQL:" + sql.toString()); //執行SQL // int rows = stat.executeUpdate(sql); int rows = stat.executeUpdate(sql.toString()); if (rows > 0) { System.out.println("execute OK!!"); } else { System.out.println("execute error !!"); } */ } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (stat != null) { stat.close(); } } catch (SQLException e) { e.printStackTrace(); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { e.printStackTrace(); } } } } private static Object getInner(Object conn) { Object object = null; Field f ; try { f = conn.getClass().getDeclaredField("inner"); f.setAccessible(true); object = f.get(conn); f.setAccessible(false); } catch (Exception e) { e.printStackTrace(); } return object; } }
結果:
首先,從數據庫連接池獲得一個連接。發現連接類型并不是mysql的數據庫連接,而是,com.mchange.v2.c3p0.impl.NewProxyConnection。通過類名,可以推測,從數據庫連接池中獲取的只是一個代理。
當我們關閉.NewProxyConnection連接時,并沒有真正關閉連接,而只是將數據庫連接放入連接池保存,使得數據庫連接在連接池中得到復用。而從連接池返回的NewProxyConnection對象,只是對真實數據庫連接的包裝。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。