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