package druidConnect;
|
|
import com.alibaba.druid.pool.DruidDataSourceFactory;
|
|
import javax.sql.DataSource;
|
import java.io.IOException;
|
import java.sql.Connection;
|
import java.sql.ResultSet;
|
import java.sql.SQLException;
|
import java.sql.Statement;
|
import java.util.Properties;
|
|
public class JDBCUtils{
|
//1.¶¨Òå³ÉÔ±±äÁ¿
|
private static DataSource dataSource;
|
|
static {
|
|
try {
|
//1.¼ÓÔØÅäÖÃÎļþ
|
Properties properties = new Properties();
|
properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties"));
|
//System.out.println(properties);
|
//2.»ñÈ¡DataSource
|
dataSource = DruidDataSourceFactory.createDataSource(properties);
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
/**
|
* »ñÈ¡Á¬½Ó
|
*/
|
public static Connection getConnection() throws Exception {
|
return dataSource.getConnection();
|
}
|
|
/**
|
* ÊÍ·Å×ÊÔ´
|
*/
|
public static void close(Statement stmt, Connection conn){
|
close(null, stmt, conn);
|
}
|
/**
|
* ÊÍ·Å×ÊÔ´
|
*/
|
public static void close(ResultSet rs, Statement stmt, Connection conn){
|
if (rs != null){
|
try {
|
rs.close();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
if (stmt != null){
|
try {
|
stmt.close();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
if (conn != null){
|
try {
|
conn.close();
|
} catch (SQLException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
/**
|
* »ñÈ¡Á¬½Ó³Ø
|
*/
|
public static DataSource getDataSource(){
|
return dataSource;
|
}
|
|
public static void main(String[] args) throws SQLException {
|
//getDataSource();
|
}
|
}
|