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();
}

}
}

}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。