编写通用方法(通过配置文件)
public class JDBCUtil {
// 定义四个成员变量,作为连接数据库的四要素
private static String driver;
private static String url;
private static String username;
private static String password;
// 静态代码块,注册驱动
static {
try {
Properties properties = new Properties();
InputStream resourceAsStream = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
properties.load(resourceAsStream); // 加载配置文件中的数据
// 给成员属性赋值,通过properties对象
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
password = properties.getProperty("password");
// 注册驱动
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {