JDBC配置文件的读取方式-非静态方法和静态方法

非静态方法读取

public void getConnection2() throws IOException{
InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc.properties");
Properties pro = new Properties();
pro.load(is);
System.out.println(pro.getProperty("username"));
}

但是静态方法不可以,静态方法可以通过如下方式:

public static Connection getConnection() {
try {

//关键两行代码
ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
System.out.println(bundle.getString("username"));//可以读取key对应的value


//Class.forName(DRIVER_CLASS);
//conn = DriverManager.getConnection(DATABASE_URL,DATABASE_USRE, DATABASE_PASSWORD);
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}

猜你喜欢

转载自blog.csdn.net/kongfanyu/article/details/53044883