WzwJDBC 自定义工具类(获取连接,释放资源)

package wzwUtil;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class WzwJDBC {

private static String driverClass;
private static String url;
private static String user;
private static String password;

static {
InputStream is = null;
try {
//获取配置文件中的数据
Properties p = new Properties();
//获取内容
is = WzwJDBC.class.getClassLoader().getResourceAsStream("wzwProp.properties");
//加载
p.load(is);
//赋值
driverClass = p.getProperty("driverClass");
url = p.getProperty("url");
user = p.getProperty("user");
password = p.getProperty("password");

//注册
Class.forName(driverClass);

} catch (Exception e) {
e.printStackTrace();
}finally{
try {
is.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}

}

public static Connection getConncetion() throws Exception {
//获取连接
Connection connection = DriverManager.getConnection(url, user, password);
return connection;
}

public static void close(ResultSet resultSet, Statement statement,Connection connection){
try {
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (statement != null) {
statement.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
e.printStackTrace();
}

}

public static void close(Statement statement,Connection connection){
WzwJDBC.close(null,statement,connection);

}
}
posted @ 2020-09-17 12:03  骚哥  阅读(178)  评论(0编辑  收藏  举报