Java中读取配置文件使用jdbc-c3p0连接池连接sqlserver数据库
来源:互联网 发布:广告语录音软件 编辑:程序博客网 时间:2024/05/01 20:50
依赖的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.SQLServerDriverjdbcUrl=jdbc\:sqlserver\://192.163.20.17;DatabaseName\=20150106user=sapassword=sqlacquireIncrement=8initialPoolSize=10minPoolSize=3maxPoolSize=20checkoutTimeout=60000acquireRetryAttempts=30numHelperThreads=7acquireRetryDelay=1000testConnectionOnCheckin=false
0 1
- Java中读取配置文件使用jdbc-c3p0连接池连接sqlserver数据库
- jdbc连接池中c3p0的配置文件的详解以及在在java中如何使用
- java JDBC 使用C3P0连接池
- Java使用JDBC连接SQLserver数据库(一)
- Java使用JDBC连接SQLserver数据库(二)
- Java中使用JDBC连接SQLserver
- java使用c3p0连接数据库
- JDBC连接中C3P0
- JAVA中C3P0连接池的使用
- Java中使用C3P0连接池
- Java中使用C3P0连接池
- Java中使用C3P0连接池
- Spring -- spring 中使用jdbc, c3p0连接池, dao概念
- JDBC连接池C3P0
- JDBC连接池C3P0
- JDBC连接池C3P0
- jdbc连接Sqlserver数据库
- JDBC 连接 sqlserver数据库
- Android5.0网络之socket接口的改变
- tc/netem
- SAT数学基本词汇(代数)
- Javascript--cookie创建与查看
- CodeIgniter笔记
- Java中读取配置文件使用jdbc-c3p0连接池连接sqlserver数据库
- IOS学习实例之三---QQ列表
- printk 使用方法
- 终极版android.mk
- Struts2 防止重复提交问题
- 浅谈 SOAP
- Convolutional Neural Networks for Visual Recognition 5
- jquery实现替代iframe的功能
- 深度学习笔记9 Sparse coding