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);
}
}
* 博客文章部分截图及内容来自于学习的书本及相应培训课程,仅做学习讨论之用,不做商业用途。
* 如有侵权,马上联系我,我立马删除对应链接。
* 备注:王子威
* 我的网易邮箱:wzw_1314_520@163.com