【自用】重构常用代码

 

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"));

posted @ 2024-07-19 16:38  白泽的格物志  阅读(17)  评论(0)    收藏  举报