Properties配置文件

xiaoxiao2021-02-28  115

使用配置文件访问数据库的优点是:

一次编写随时调用,数据库类型发生变化只需要修改配置文件。

配置文件的设置:

在配置文件中,key-value对应的方式编写。

读取配置文件:

使用Properties对象的load()方法来实现配置文件的读取,使用流来实现文件读写的操作。

具体配置代码如下:

Properties prop=new Properties(); String configFile="jdbc.properties";//配置文件路径 InputStream is=Test.class.getClassLoader().getResourceAsStream(configFile);//加载配置文件到输入流中 try { prop.load(is);//从输入流中读取属性列表 } catch (IOException e) { e.printStackTrace(); } //根据指定的获取对应的值 String driver=prop.getProperty("driver"); String url=prop.getProperty("url"); String user=prop.getProperty("user"); String pwd=prop.getProperty("password"); }
转载请注明原文地址: https://www.6miu.com/read-39570.html

最新回复(0)