Java基础之 连接池 dbcp c3p0

dbcp连接池

	//dbcp连接池--直接读取dbcp.properties配置文件
	public Connection dbcpDataSourceByFactory() throws Exception{
		//1.从配置文件中读取配置参数
		Properties properties = new Properties();
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("dbcp.properties");
		properties.load(in);

		//2.从BasicDataSourceFactory中获取
		BasicDataSource dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties);
		
		//3.从数据库连接池中获取链接
		Connection conn = dataSource.getConnection();
		return conn;
	}
	
	//dbcp连接池--直接设置参数
	public Connection dbcpDataSource() throws SQLException{
		BasicDataSource dataSource = null;
		//1.创建dbcp数据源实例
		dataSource = new BasicDataSource();
		
		//2.设置必需的参数
		dataSource.setUsername("root");
		dataSource.setPassword("123");
		dataSource.setUrl("jdbc:mysql:\\localhost:3316\test");
		dataSource.setDriverClassName("com.mysql.jdcb.Driver");
		
		//3.设置配置参数
		//1) 数据库连接池中,初始化连接数的个数
		dataSource.setInitialSize(5);
		
		//2) 最大连接数
		dataSource.setMaxActive(10);
		
		//3) 最小连接数
		dataSource.setMinIdle(5);
		
		//4) 等待数据库连接池分配连接的最长时间
		dataSource.setMaxActive(10);
		
		//从数据库连接池中获取链接
		Connection conn = dataSource.getConnection();
		return conn;
	}

dbcp.properties

username=root
password=1230
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test

initialSize=10
maxActive=50
minIdle=5
maxWait=5000

c3p0链接池
    //c3p0链接池 -- 读取c3p0-config.xml 配置文件
    public Connection c3p0DataSourceByXml() throws Exception {
        //1.通过c3p0-config.xml 创建 ComboPooledDataSource 实例          
        ComboPooledDataSource cpds = new ComboPooledDataSource("hdc3p0");
        
        //2.从数据库连接池中获取链接
        Connection conn = cpds.getConnection();
        return conn;
    }
    //c3p0链接池 --- 直接设置参数

    public Connection c3p0DataSource() throws Exception {
        //1.创建 ComboPooledDataSource 实例
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        
        //2.设置参数
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql:///atguigu");
        cpds.setUser("root");
        cpds.setPassword("1230");
        
        //3.从数据库连接池中获取链接
        Connection conn = cpds.getConnection();
        return conn;
    }


c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

	<named-config name="hdc3p0">
		
		<!-- 指定连接数据源的基本属性 -->
		<property name="user">root</property>
		<property name="password">1230</property>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql:///atguigu</property>
		
		<!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
		<property name="acquireIncrement">5</property>
		<!-- 初始化数据库连接池时连接的数量 -->
		<property name="initialPoolSize">5</property>
		<!-- 数据库连接池中的最小的数据库连接数 -->
		<property name="minPoolSize">5</property>
		<!-- 数据库连接池中的最大的数据库连接数 -->
		<property name="maxPoolSize">10</property>

		<!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
		<property name="maxStatements">20</property>
		<!-- 每个连接同时可以使用的 Statement 对象的个数 -->
		<property name="maxStatementsPerConnection">5</property>
	
	</named-config>
		
</c3p0-config>


dbcp连接池

	//dbcp连接池--直接读取dbcp.properties配置文件
	public Connection dbcpDataSourceByFactory() throws Exception{
		//1.从配置文件中读取配置参数
		Properties properties = new Properties();
		InputStream in = this.getClass().getClassLoader().getResourceAsStream("dbcp.properties");
		properties.load(in);

		//2.从BasicDataSourceFactory中获取
		BasicDataSource dataSource = (BasicDataSource) BasicDataSourceFactory.createDataSource(properties);
		
		//3.从数据库连接池中获取链接
		Connection conn = dataSource.getConnection();
		return conn;
	}
	
	//dbcp连接池--直接设置参数
	public Connection dbcpDataSource() throws SQLException{
		BasicDataSource dataSource = null;
		//1.创建dbcp数据源实例
		dataSource = new BasicDataSource();
		
		//2.设置必需的参数
		dataSource.setUsername("root");
		dataSource.setPassword("123");
		dataSource.setUrl("jdbc:mysql:\\localhost:3316\test");
		dataSource.setDriverClassName("com.mysql.jdcb.Driver");
		
		//3.设置配置参数
		//1) 数据库连接池中,初始化连接数的个数
		dataSource.setInitialSize(5);
		
		//2) 最大连接数
		dataSource.setMaxActive(10);
		
		//3) 最小连接数
		dataSource.setMinIdle(5);
		
		//4) 等待数据库连接池分配连接的最长时间
		dataSource.setMaxActive(10);
		
		//从数据库连接池中获取链接
		Connection conn = dataSource.getConnection();
		return conn;
	}

dbcp.properties

username=root
password=1230
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///test

initialSize=10
maxActive=50
minIdle=5
maxWait=5000

c3p0链接池
    //c3p0链接池 -- 读取c3p0-config.xml 配置文件
    public Connection c3p0DataSourceByXml() throws Exception {
        //1.通过c3p0-config.xml 创建 ComboPooledDataSource 实例          
        ComboPooledDataSource cpds = new ComboPooledDataSource("hdc3p0");
        
        //2.从数据库连接池中获取链接
        Connection conn = cpds.getConnection();
        return conn;
    }
    //c3p0链接池 --- 直接设置参数

    public Connection c3p0DataSource() throws Exception {
        //1.创建 ComboPooledDataSource 实例
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        
        //2.设置参数
        cpds.setDriverClass("com.mysql.jdbc.Driver");
        cpds.setJdbcUrl("jdbc:mysql:///atguigu");
        cpds.setUser("root");
        cpds.setPassword("1230");
        
        //3.从数据库连接池中获取链接
        Connection conn = cpds.getConnection();
        return conn;
    }


c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

	<named-config name="hdc3p0">
		
		<!-- 指定连接数据源的基本属性 -->
		<property name="user">root</property>
		<property name="password">1230</property>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql:///atguigu</property>
		
		<!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
		<property name="acquireIncrement">5</property>
		<!-- 初始化数据库连接池时连接的数量 -->
		<property name="initialPoolSize">5</property>
		<!-- 数据库连接池中的最小的数据库连接数 -->
		<property name="minPoolSize">5</property>
		<!-- 数据库连接池中的最大的数据库连接数 -->
		<property name="maxPoolSize">10</property>

		<!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
		<property name="maxStatements">20</property>
		<!-- 每个连接同时可以使用的 Statement 对象的个数 -->
		<property name="maxStatementsPerConnection">5</property>
	
	</named-config>
		
</c3p0-config>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值