Java中读取配置文件使用jdbc-c3p0连接池连接sqlserver数据库

本文介绍了一种使用 C3P0 实现的 Java 数据库连接池管理方案。通过读取配置文件,初始化连接池参数,并提供获取连接及资源释放的方法。适用于需要频繁访问数据库的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

依赖的jar包有c3p0-0.9.2-pre1.jar
mchange-commons-0.2.jar
sqljdbc4.jar。
使用的是读取配置文件的方式读取配置信息。
不多说了,直接上代码。

连接代码:
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class JdbcUtils_C3P0 {
	
	private static ComboPooledDataSource ds = null;
	
	static{
		try{
			ds = new ComboPooledDataSource();
			FileInputStream in = new FileInputStream(new File("configuration/jdbc.properties"));
			Properties prop = new Properties();
			prop.load(in);
			String driverClass = prop.getProperty("driverClass");
			String jdbcUrl = prop.getProperty("jdbcUrl");
			String user = prop.getProperty("user");
			String password = prop.getProperty("password");
			String acquireIncrement = prop.getProperty("acquireIncrement");
			String initialPoolSize = prop.getProperty("initialPoolSize");
			String minPoolSize = prop.getProperty("minPoolSize");
			String maxPoolSize = prop.getProperty("maxPoolSize");
			String checkoutTimeout = prop.getProperty("checkoutTimeout");
			String acquireRetryAttempts = prop.getProperty("acquireRetryAttempts");
			String acquireRetryDelay = prop.getProperty("acquireRetryDelay");
			String testConnectionOnCheckin = prop.getProperty("testConnectionOnCheckin");
			
			ds.setDriverClass(driverClass);
			ds.setJdbcUrl(jdbcUrl);
			ds.setUser(user);
			ds.setPassword(password);
			ds.setAcquireIncrement(Integer.parseInt(acquireIncrement));
			ds.setInitialPoolSize(Integer.parseInt(initialPoolSize));
			ds.setMinPoolSize(Integer.parseInt(minPoolSize));
			ds.setMaxPoolSize(Integer.parseInt(maxPoolSize));
			ds.setCheckoutTimeout(Integer.parseInt(checkoutTimeout));
			ds.setAcquireRetryAttempts(Integer.parseInt(acquireRetryAttempts));
			ds.setAcquireRetryDelay(Integer.parseInt(acquireRetryDelay));
			ds.setTestConnectionOnCheckin(Boolean.valueOf(testConnectionOnCheckin));
			
		}catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
	}
	
	public static Connection getConnection() throws SQLException{
		return ds.getConnection();
	}
	
	public static void release(Connection conn,PreparedStatement pstmt,ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			rs = null;
		}
		if(pstmt!=null){
			try {
				pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			pstmt = null;
		}
		
		if(conn!=null) {
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			conn = null;
		}
	}
}

配置文件jdbc.properties

driverClass=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbcUrl=jdbc\:sqlserver\://192.163.20.17;DatabaseName\=20150106
user=sa
password=sql
acquireIncrement=8
initialPoolSize=10
minPoolSize=3
maxPoolSize=20
checkoutTimeout=60000
acquireRetryAttempts=30
numHelperThreads=7
acquireRetryDelay=1000
testConnectionOnCheckin=false
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值