String driverName = “”;
String url = “”;
String username = “”;
String password = “”;
//获取数据库配置文件方法一:
// Properties p = new Properties();
// InputStream in = this.getClass().getClassLoader().getResourceAsStream(“jdbc.properties”);
// try {
// p.load(in);
// in.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// driverName= p.getProperty(“jdbc.driverClassName”);
// url = p.getProperty(“jdbc.url”);
// username = p.getProperty(“jdbc.username”);
// password = p.getProperty(“jdbc.password”);
//获取数据库配置文件方法二:
// ResourceBundle rb = ResourceBundle.getBundle(“jdbc”);
//获取数据库配置文件方法三:
InputStream in = null;
ResourceBundle rb = null;
try {
// 该方法必须指明准确的文件地址,否则编译不通过
String fileLocation = this.getClass().getClassLoader().getResource(“jdbc.properties”).getPath();
in = new BufferedInputStream(new FileInputStream(fileLocation));
rb = new PropertyResourceBundle(in);
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
driverName = rb.getString(“jdbc.driverClassName”);
url = rb.getString(“jdbc.url”);
username = rb.getString(“jdbc.username”);
password = rb.getString(“jdbc.password”);
java获取properties配置文件
最新推荐文章于 2023-12-08 14:13:20 发布