自定义JdbcUtils
package jdbc;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class JdbcUtils {
public static Connection getConnection() {
Connection connection = null;
try {
InputStream resource = JdbcPreparedStatementTest.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(resource);
String username = properties.getProperty("username");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
String driverClassName = properties.getProperty("driverClassName");
Class.forName(driverClassName);
connection = DriverManager.getConnection(url, username, password);
} catch (IOException | ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
public static void close(Connection connection, Statement statement) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
public static void close(Connection connection, Statement statement, ResultSet resultSet) {
try {
if (connection != null) {
connection.close();
}
if (statement != null) {
statement.close();
}
if (resultSet != null) {
resultSet.close();
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}