public class JDBCUtil {
private static String driver;
private static String url;
private static String user;
private static String password;
private static Object Connection;
static {
//读取配置问
try {
Properties pro = new Properties();
InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
pro.load(is);
driver = pro.getProperty("driver");
System.out.println(driver);
url = pro.getProperty("url");
System.out.println(url);
user = pro.getProperty("user");
System.out.println(user);
password = pro.getProperty("password");
System.out.println(password);
Class.forName(driver);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection () throws SQLException {
return DriverManager.getConnection(url,user,password);
}
//释放资源DML
public static void close (Connection connection, Statement statement){
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
//DQL
public static void close (Connection connection, Statement statement, ResultSet resultSet){
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}