数据库连接池

开源的数据库连接池使用测试(C3P0&Druid)

C3P0数据库连接池

1.导入支持的jar包(数据库连接池驱动与mySql驱动)
在这里插入图片描述
2.使用C3P0数据库连接池的配置文件方式,获取数据库的连接

  • 配置文件(src下):c3p0-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>

 <!-- default-config 默认的配置,使用C3P0数据库连接池的配置文件方式,获取数据库的连接,通过ComboPooledDataSource cpds = new ComboPooledDataSource(); ComboPooledDataSource()空参时默认读取的是 default-config 默认的配置
 -->  
  <default-config>
 	 <!-- 提供获取连接的4个基本信息 -->
  	<property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost/stusmanager</property>
    <property name="user">root</property>
    <property name="password">1998</property>
  
    <property name="automaticTestTable">con_test</property>
    <property name="checkoutTimeout">30000</property>
    <property name="idleConnectionTestPeriod">30</property>
     <!-- 初始化数据库连接池时连接的数量 -->
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <!-- 数据库连接池中的最大的数据库连接数 -->
    <property name="maxPoolSize">100</property>
     <!-- 数据库连接池中的最小的数据库连接数 -->
    <property name="minPoolSize">10</property>
    <property name="maxStatements">200</property>

    <user-overrides user="test-user">
      <property name="maxPoolSize">10</property>
      <property name="minPoolSize">1</property>
    	 <!-- C3P0 数据库连接池可以维护的 Statement 的个数 -->
      <property name="maxStatements">0</property>
    </user-overrides>
  </default-config>


  <!-- This app is massive! 其他配置,new ComboPooledDataSource("oracle");参数不为空时,根据参数(name="oracle")来读取配置信息-->
  <named-config name="oracle"> 
    <property name="acquireIncrement">50</property>
    <property name="initialPoolSize">100</property>
    <property name="minPoolSize">50</property>
    <property name="maxPoolSize">1000</property>

    <!-- intergalactoApp adopts a different approach to configuring statement caching -->
    <property name="maxStatements">0</property> 
    <property name="maxStatementsPerConnection">5</property>

    <!-- he's important, but there's only one of him -->
    <user-overrides user="master-of-the-universe"> 
      <!-- 若数据库中连接数不足时, 一次向数据库服务器申请多少个连接 -->
      <property name="acquireIncrement">1</property>
      <property name="initialPoolSize">1</property>
      <property name="minPoolSize">1</property>
      <property name="maxPoolSize">5</property>
      <property name="maxStatementsPerConnection">50</property>
    </user-overrides>
  </named-config>
  
</c3p0-config>
  • 代码测试(获取连接并执行crud操作)
public class TestC3p0 {
	@Test
	public void testC3p0(){
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		try {
			//使用C3P0数据库连接池的配置文件方式,获取数据库的连接,使用default-config配置中名为oracle的配置信息
			//ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("oracle");
			
			//空参默认使用default-config 默认的配置 
			ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();

			//DataSource 通常被称为数据源,它包含连接池和连接池管理两个部分,习惯上也把 DataSource 称为连接池。
			
			//得到连接对象
			connection = comboPooledDataSource.getConnection();


			String sql = "insert into t_students values(null,?,?,?)";
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setString(1, "老七");
			prepareStatement.setInt(2, 22);
			prepareStatement.setString(3, "男");
			prepareStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(connection!=null){
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(prepareStatement!=null){
					prepareStatement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

}
Druid(德鲁伊)数据库连接池

1.导入支持的jar包(数据库连接池驱动与mySql驱动)
在这里插入图片描述
2.使用Druid数据库连接池的配置文件方式,获取数据库的连接

  • 配置文件(src下):druid.properties
url=jdbc:mysql:///stusmanager
username=root
password=1998
driverClassName=com.mysql.jdbc.Driver
initialSize=10
maxActive=10
  • 代码测试(获取连接并执行crud操作)
public class TestDruid {
	@Test
	public void testDruid(){
		Connection connection = null;
		PreparedStatement prepareStatement = null;
		try {
			Properties pros = new Properties();
			InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
			pros.load(is);
			DataSource createDataSource = DruidDataSourceFactory.createDataSource(pros);
			//得到连接对象
			connection = createDataSource.getConnection();


			String sql = "insert into t_students values(null,?,?,?)";
			prepareStatement = connection.prepareStatement(sql);
			prepareStatement.setString(1, "老八");
			prepareStatement.setInt(2, 23);
			prepareStatement.setString(3, "男");
			prepareStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(connection!=null){
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			try {
				if(prepareStatement!=null){
					prepareStatement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}

把通过数据库连接池获取数据库连接的操作封装成一个工具类(JDBCUtils.java)

public class JDBCUtils {
	//使用C3P0的数据库连接池技术获取数据库连接
	/*数据库连接池只需提供一个即可。
	 	写法一:
		private static ComboPooledDataSource comboPooledDataSource=null;
		static{
		 	comboPooledDataSource = new ComboPooledDataSource();
		}
	 */
	
	//写法二:
	private static ComboPooledDataSource comboPooledDataSource=new ComboPooledDataSource();

	public static Connection getConnection() throws SQLException{
		return comboPooledDataSource.getConnection();	
	}
	
	
	//使用Druid数据库连接池技术获取数据库连接
	//数据库连接池只需提供一个
	private static DataSource createDataSource;
	static{
		try {
			Properties pros = new Properties();
			InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
			pros.load(is);
			createDataSource = DruidDataSourceFactory.createDataSource(pros);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	//得到连接的方法
	public static Connection getConnection1() throws SQLException{
		return createDataSource.getConnection();	
	}
    //得到数据源的方法
	public static DataSource getDataSource() {
		return createDataSource;
	}
	
}

一个较完整的JDBCUtils工具类

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Thinking in Coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值