package cn.com.QueryRunner;
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;
public class JDBCUtilsConfig {
private static Connection con;
private static String driverClass;
private static String url;
private static String username;
private static String password;
static{
try {
readConfig();
Class.forName(driverClass);
con=DriverManager.getConnection(url,username,password);
} catch (Exception e) {
//throw new RuntimeException("数据库连接失败");
e.printStackTrace();
}
}
//读配置文件
private static void readConfig() throws Exception{
//类加载器
InputStream in = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
//这里的database.properties里面是配置文件
Properties pro = new Properties();
pro.load(in);
driverClass = pro.getProperty("driverClass");
url = pro.getProperty("url");
username = pro.getProperty("username");
password = pro.getProperty("password");
}
public static Connection getConection(){
return con;
}
public static void closeAll(Connection conn,Statement st,ResultSet rs){
//负责关闭
if(conn != null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(st != null){
try {
st.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs != null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}