package com.yjm.c3p0; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; /** * * @author yjm18 * c3p0 创建与管理 jdbc数据库连接池 * */ public class ConnectionFactoryc3p0 { public static ComboPooledDataSource comboPooledDataSource = null; static { // 初始化 c3p0 JDBC数据连接与控制池 comboPooledDataSource = new ComboPooledDataSource(); try { comboPooledDataSource.setDriverClass("com.mysql.jdbc.Driver"); } catch (PropertyVetoException e) { e.printStackTrace(); } comboPooledDataSource .setJdbcUrl("jdbc:mysql://localhost:3306/itruscnoocgasmock?useUnicode=true&characterEncoding=UTF-8"); comboPooledDataSource.setUser("root"); comboPooledDataSource.setPassword("1234"); comboPooledDataSource.setMinPoolSize(20); comboPooledDataSource.setMaxPoolSize(50); } public static synchronized Connection getC3p0Connection() { Connection con=null; try { con= comboPooledDataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } finally { return con; } } public static void main(String[] args) { try { //通过c3p0进行查询操作 ResultSet rs = ConnectionFactoryc3p0.getC3p0Connection() .prepareStatement("select * from signinfo").executeQuery(); while (rs.next()) { System.out.println(rs.getString(1)); } } catch (SQLException e) { e.printStackTrace(); } System.out.println(); } }
不用框架管理 jdbc数据连接池。增加 增删改查处理效率。
时间: 2024-10-18 19:28:17