抄写
package tools;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class DbHelper {
public static final Properties PROPERTIES = new Properties();
static {
InputStream is = DbHelper.class.getResourceAsStream("config.properties");
try {
PROPERTIES.load(is);
} catch (IOException e) {
e.printStackTrace();
}
try {
Class.forName(PROPERTIES.getProperty("driver"));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() {
String url = PROPERTIES.getProperty("url");
String user = PROPERTIES.getProperty("user");
String pwd = PROPERTIES.getProperty("pwd");
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, pwd);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return connection;
}
public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
if(resultSet!=null){
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(statement!=null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if(connection!=null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}