熊咪

javaweb-dbcp2

package cn.itcast.utils;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;


public class JdbcUtils_DBCP {

private static DataSource ds = null;
static{
try{
InputStream in = JdbcUtils_DBCP.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");
Properties prop = new Properties();
prop.load(in);

BasicDataSourceFactory factory = new BasicDataSourceFactory();

ds = factory.createDataSource(prop);
System.out.println(ds);
}catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}

public static Connection getConnection() throws SQLException{

return ds.getConnection();
}

public static void release(Connection conn,Statement st,ResultSet rs){


if(rs!=null){
try{
rs.close();
}catch (Exception e) {
e.printStackTrace();
}
rs = null;

}
if(st!=null){
try{
st.close();
}catch (Exception e) {
e.printStackTrace();
}

}

if(conn!=null){
try{
conn.close();
}catch (Exception e) {
e.printStackTrace();
}

}
}

}

posted on 2015-05-19 10:14  熊咪  阅读(180)  评论(3编辑  收藏  举报