JDBC使用反射读取properties文件方法笔记
2016-07-15 10:33
519 查看
1,配置文件放到src下
2,代码片段
public class CMConstant {
public static String getConfigureParameterFromJDBC(String paramString) {
String str = CMConstant.getRootPath() + File.separator + "WEB-INF"
+ File.separator + "classes/config.properties";
Properties localProperties = new Properties();
try {
FileInputStream localFileInputStream = new FileInputStream(str);
localProperties.load(localFileInputStream);
localFileInputStream.close();
} catch (FileNotFoundException localFileNotFoundException) {
logger.error("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
localFileNotFoundException.printStackTrace();
return null;
} catch (IOException localIOException) {
logger.error("装载文件--->失败!");
localIOException.printStackTrace();
}
return localProperties.getProperty(paramString);
}
public static String getRootPath() {
String result = null;
try {
result = CMConstant.class.getResource("CMConstant.class").toURI()
.getPath().toString();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
int index = result.indexOf("WEB-INF");
if (index == -1) {
index = result.indexOf("bin");
}
result = result.substring(1, index);
if (result.endsWith("/"))
result = result.substring(0, result.length() - 1);// 不包含最后的"/"
return result;
}
<span style="white-space:pre"> </span>/**
* 查找config/jdbc.properties里值
* @Description: @param key
* @Description: @return
* @Last Modified: , Date Modified:
*/
public static String getJDBCValue(String key) {
String filePath = getRootPath() + File.separator + "WEB-INF\\classes"
+ File.separator + "config.properties";
Properties propertie = new Properties();
try {
FileInputStream inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
return null;
} catch (IOException ex) {
ex.printStackTrace();
}
return propertie.getProperty(key);
}
}
2,代码片段
public class CMConstant {
public static String getConfigureParameterFromJDBC(String paramString) {
String str = CMConstant.getRootPath() + File.separator + "WEB-INF"
+ File.separator + "classes/config.properties";
Properties localProperties = new Properties();
try {
FileInputStream localFileInputStream = new FileInputStream(str);
localProperties.load(localFileInputStream);
localFileInputStream.close();
} catch (FileNotFoundException localFileNotFoundException) {
logger.error("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
localFileNotFoundException.printStackTrace();
return null;
} catch (IOException localIOException) {
logger.error("装载文件--->失败!");
localIOException.printStackTrace();
}
return localProperties.getProperty(paramString);
}
public static String getRootPath() {
String result = null;
try {
result = CMConstant.class.getResource("CMConstant.class").toURI()
.getPath().toString();
} catch (URISyntaxException e1) {
e1.printStackTrace();
}
int index = result.indexOf("WEB-INF");
if (index == -1) {
index = result.indexOf("bin");
}
result = result.substring(1, index);
if (result.endsWith("/"))
result = result.substring(0, result.length() - 1);// 不包含最后的"/"
return result;
}
<span style="white-space:pre"> </span>/**
* 查找config/jdbc.properties里值
* @Description: @param key
* @Description: @return
* @Last Modified: , Date Modified:
*/
public static String getJDBCValue(String key) {
String filePath = getRootPath() + File.separator + "WEB-INF\\classes"
+ File.separator + "config.properties";
Properties propertie = new Properties();
try {
FileInputStream inputFile = new FileInputStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
return null;
} catch (IOException ex) {
ex.printStackTrace();
}
return propertie.getProperty(key);
}
}
相关文章推荐
- jdbc中的Statement和PreparedStatement接口对象
- JDBC 数据库常用连接 链接字符串
- JDBC连接Access数据库的几种方式介绍
- 基于JDBC封装的BaseDao(实例代码)
- JDBC程序更新数据库中记录的方法
- jdbc链接远程数据库进行修改url操作
- JDBC 程序的常见错误及调试方法
- 在Java的JDBC使用中设置事务回滚的保存点的方法
- Java中使用JDBC操作数据库简单实例
- Java加载JDBC驱动程序实例详解
- JSP使用JDBC完成动态验证及采用MVC完成数据查询的方法
- JSP基于JDBC的数据库连接类实例
- JSP中使用JDBC访问SQL Server 2008数据库示例
- jsp+jdbc实现连接数据库的方法
- 解析jdbc处理oracle的clob字段的详解
- JDBC数据库的使用操作总结
- jdbc操作mysql数据库实例
- JSP使用JDBC连接MYSQL数据库的方法
- Java开发Oracle数据库连接JDBC Thin Driver 的三种方法
- java使用jdbc操作数据库示例分享