【自用】重构常用代码
private static final String CONFIG_FILE = "database.properties"; // 配置文件路径
private static Properties dbProperties;
static {
// 静态代码块,用于加载配置文件
dbProperties = new Properties();
try (FileInputStream in = new FileInputStream(CONFIG_FILE)) {
dbProperties.load(in);
} catch (IOException e) {
throw new RuntimeException("无法加载数据库配置文件", e);
}
Class.forName(dbProperties.getProperty("driverClassName"));
conn = DriverManager.getConnection(dbProperties.getProperty("url"), dbProperties.getProperty("username"), dbProperties.getProperty("pssword"));
浙公网安备 33010602011771号