一、c3p0連接池 1.導包(lib 下) 資料庫連接池 c3p0-0.9.5.2.jar machange-commons-java-0.2.11.jar MySQL驅動 mysql-connector-java-8.0.11.jar 2.核心方法 // 核心連接池類 ComboPooledDat ...
一、c3p0連接池
1.導包(lib 下)
資料庫連接池c3p0-0.9.5.2.jar machange-commons-java-0.2.11.jar MySQL驅動 mysql-connector-java-8.0.11.jar
2.核心方法
// 核心連接池類
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
// 設置四個JDBC基本連接屬性
comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver");
comboPooledDataSource.setJdbcUrl("jdbc:mysql:///day04");
comboPooledDataSource.setUser("root");
comboPooledDataSource.setPassword("123");
3.jdbc.properties配置文件
driverClass=com.mysql.cj.jdbc.Driver url=jdbc:mysql://localhost:3306/mysql?serverTimezone=UTC&characterEncoding=utf-8 username=root password=111
4.JDBCUtils工具類
import java.io.FileReader; import java.io.IOException; import java.sql.*; import java.util.Properties; public class JDBCUtils { private static final String driverClass; private static final String url; private static final String username; private static final String password; static { Properties prop = new Properties(); try { prop.load(new FileReader("jdbc.properties")); driverClass = prop.getProperty("driverClass"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); // 載入驅動 loadDriver(); } catch (IOException e) { // e.printStackTrace(); throw new RuntimeException("配置文件載入失敗!"); } } // 載入驅動 public static void loadDriver() { try { Class.forName(driverClass); } catch (ClassNotFoundException e) { // e.printStackTrace(); throw new RuntimeException("驅動載入失敗!"); } } // 獲取連接 public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, username, password); } // 釋放資源 public static void release(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } release(conn, stmt); } public static void release(Connection conn, Statement stmt) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } } }
// 測試類 public class JDBCTemplateTest1 { @Test public void test1() throws SQLException, PropertyVetoException { // 需求 : 查詢 user 表中的所有數據 // 核心連接池類 ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 設置四個JDBC基本連接屬性 dataSource.setDriverClass("com.mysql.cj.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC&characterEncoding=utf-8"); dataSource.setUser("root"); dataSource.setPassword("111"); Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { // 1. 建立連接 conn = dataSource.getConnection(); // 2. 操作數據 String sql = "select * from user;"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String username = rs.getString("username"); String password = rs.getString("password"); String email = rs.getString("email"); System.out.println(id + " : " + username + " : " + password + " : " + email); } } catch (SQLException e) { e.printStackTrace(); } finally { // 3. 釋放資源 JDBCUtils.release(conn, stmt, rs); } } }
5.通過xml 獲取配置信息
ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); 會自定載入配置文件
常用基本連接池屬性
acquireIncrement 如果連接池中連接都被使用了,一次性增長3個新的連接
initialPoolSize 連接池中初始化連接數量預設:3
maxPoolSize 最大連接池中連接數量預設:15連接
maxIdleTime 如果連接長時間沒有時間,將被回收預設:0 連接永不過期
minPoolSize 連接池中最小連接數量 預設:3
c3p0-config.xml 資料庫連接池配置文件 : 需要創建在 src 目錄下. <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 預設配置,c3p0框架預設載入這段預設配置 --> <default-config> <!-- 配置JDBC 四個基本屬性 --> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC&characterEncoding=utf-8</property> <property name="user">root</property> <property name="password">111</property> </default-config> <!-- 可以自定義配置,為這段配置起一個名字,c3p0指定名稱載入配置 --> <named-config name="xxxxx"> <property name="driverClass">com.mysql.cj.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb?serverTimezone=UTC&characterEncoding=utf-8</property> <property name="user">root</property> <property name="password">111</property> </named-config> </c3p0-config>c
@Test public void test_c3p0() throws PropertyVetoException { // 需求 : 查詢 user 表中的所有數據 // 核心連接池類 ComboPooledDataSource dataSource = new ComboPooledDataSource(); Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { // 1. 建立連接 conn = dataSource.getConnection(); // 2. 操作數據 String sql = "select * from user;"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String username = rs.getString("username"); String password = rs.getString("password"); String email = rs.getString("email"); System.out.println(id + " : " + username + " : " + password + " : " + email); } } catch (SQLException e) { e.printStackTrace(); } finally { // 3. 釋放資源 JDBCUtils.release(conn, stmt, rs); } }
優化版的JDBCUtils 中的getConnectio 使用資料庫連接池對象方式實現
public class JDBCUtils { // c3p0 資料庫連接池對象屬性 private static final ComboPooledDataSource dataSource = new ComboPooledDataSource(); // 獲取連接 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } // 釋放資源 public static void release(Connection conn, Statement stmt, ResultSet rs) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } rs = null; } release(conn, stmt); } public static void release(Connection conn, Statement stmt) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } stmt = null; } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } conn = null; } } }
@Test public void test_jdbcUtils() { // 需求 : 查詢 user 表中的所有數據 Connection conn = null; PreparedStatement stmt = null; ResultSet rs = null; try { // 1. 建立連接 conn = JDBCUtils.getConnection(); // 2. 操作數據 String sql = "select * from user;"; stmt = conn.prepareStatement(sql); rs = stmt.executeQuery(); while (rs.next()) { int id = rs.getInt("id"); String username = rs.getString("username"); String password = rs.getString("password"); String email = rs.getString("email"); System.out.println(id + " : " + username + " : " + password + " : " + email); } } catch (SQLException e) { e.printStackTrace(); } finally { // 3. 釋放資源 JDBCUtils.release(conn, stmt, rs); } }