package com.yun.utility;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;
public class DBConnection {
private static String user;
private static String password;
private static String url;
static {
try {
ClassLoader classLoader = DBConnection.class.getClassLoader();
// 数据库的配置文件路径
InputStream is = classLoader.getResourceAsStream("config/prop/jdbc-mysql.properties");
Properties props = new Properties();
props.load(is);
url = props.getProperty("url");
user = props.getProperty("username");
password = props.getProperty("password");
Class.forName(props.getProperty("driverClassName"));
} catch (ClassNotFoundException e) {
throw new RuntimeException("ClassNotFound");
} catch (IOException e) {
throw new RuntimeException("IO");
}
}
public static Connection getConnection() throws Exception {
return (Connection) DriverManager.getConnection(url, user, password);
}
public static Statement getStatement() throws SQLException{
return (Statement) DriverManager.getConnection(url, user, password).createStatement();
}
public static void close(ResultSet rs, Statement stat, Connection conn) throws Exception {
if (rs != null) {
rs.close();
}
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
}
public static void close(Statement stat, Connection conn) throws Exception {
if (stat != null) {
stat.close();
}
if (conn != null) {
conn.close();
}
}
}