首先将数据以key=value形式编写入jdbc.properties文件中
然后定义一个实体类PropertiesUtil,定义属性private final String url= DataUtil.getProperty("url"); 并给出getter方法
其中定义DataUtil类:
public class DataUtil {
private static Properties prop;
static{
loadProps();
}
private synchronized static void loadProps() {
prop = new Properties();
InputStream in = null;
try {
in = DataUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
prop.load(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
logger.error("文件未找到");
} catch (IOException e) {
e.printStackTrace();
logger.error("IO异常");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
logger.error("InputStream流关闭异常");
}
}
}
}
public static String getProperty(String key){
if(null == prop) {
loadProps();
}
return prop.getProperty(key);
}
}
new PropertiesUtil().getUrl(); 既是