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

依赖的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
 

无意中发现了一个巨牛的人工智能教程,忍不住分享一下给大家。教程不仅是零基础,通俗易懂,而且非常风趣幽默,像看小说一样!觉得太牛了,所以分享给大家。点这里可以跳转到教程

你可能感兴趣的:(数据库)