WzwJDBC 自定義工具類(獲取連線,釋放資源)
阿新 • • 發佈:2020-09-17
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);
}
}