");
user = rb.getString("user");
password = rb.getString("password");
//这里加载驱动,静态代码块只加载一次
try {
//驱动加载失败异常
Class.forName(driverClass);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//获取数据库连接的方法
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(url,user,password);
}
//关闭数据库的方法
public static void closeAll(ResultSet resultSet,Statement statement,Connection connection){
//这里关闭资源前,要先做非空判断才能确定关闭资源否
if(resultSet != null){
try{
resultSet.close();
}catch (SQLException e){
throw new RuntimeException("关闭失败");
}
resultSet = null;//这里是将结果集清空,加快系统回收速度
}if(connection != null) {
try {
connection.close();
} catch (SQLException e) {
throw new RuntimeException("关闭失败");
}
connection = null;
}
if(statement != null) {
try {
statement.close();
} catch (SQLException e) {
throw new RuntimeException("关闭失败");
}
//这里清空数据处理对象,便于加快系统垃圾回收
statement = null;
}
}
}
|