JDBC使用反射读取properties文件方法笔记

news/2024/5/19 0:27:38 标签: java web, jdbc

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);
	}
}



http://www.niftyadmin.cn/n/1041707.html

相关文章

为什么devc调试时循环一下就过去了_导热油电加热器的调试步骤「创楚」分享...

购买导热油电加热器的客户&#xff0c;其实调试工作并不难&#xff0c;在调试工作之前需要做好以下简单的准备&#xff0c;提前备好水、电、管路连接、导热油、电缆线接至导热油电加热器处&#xff0c;这些都是前期的准备的工作。需要先将膨胀油箱到设备油气分流器的的阀门打开…

设计模式-解释器模式(26)

定义 解释器模式(Interpreter Pattern)是一种按照规定语法对表达式进行解析的方案&#xff0c;在项目中较少使用。 英文&#xff1a;Given a language&#xff0c;define a representation for its grammer along with an interpreter that uses the representation to interpr…

adams2015怎么把工具栏打开_Word怎么批量选中整篇文档中的所有图片?只需一个命令轻松搞定...

在使用Microsoft Word编辑文档时&#xff0c;图片是最好的说明方式。一篇Word文档中可能存在很多张图片&#xff0c;不管是对图片进行批量编辑&#xff0c;还是批量删除&#xff0c;都需要先选中这些图片。怎么一次性批量选中整篇Word文档中的图片呢&#xff1f;首先我们需要启…

qdialog隐藏关闭按钮_“一键启动”只能用来点火?这些隐藏的功能,老司机都不知道!...

现在很多高配的汽车都配有一键启动装置&#xff0c;使用过的车主们都认为这个功能十分方便&#xff0c;无需插入钥匙&#xff0c;只要轻轻一按启动键车辆就点着火了。很多车主以为一键启动就只能用来点火&#xff0c;实际上&#xff0c;它还隐藏了很多其他的功能&#xff0c;这…

java POI 实现合并单元格

合并单元格所使用的方法&#xff1a;sheet.addMergedRegion( CellRangeAddress cellRangeAddress );CellRangeAddress 对象的构造方法需要传入合并单元格的首行、最后一行、首列、最后一列。CellRangeAddress cranew CellRangeAddress(0, 3, 3, 9);怎样把数据写入合并后的单…

半深入理解Java属性继承

前几天在面试的时候又被问到了一个问题&#xff0c;“java重写和重载有什么区别&#xff1f;”。 这个问题在Java领域是一个老生常谈的问题了&#xff0c;事实上我认为这两个东东除了中文名长得很像以外&#xff08;英文名好像也很像&#xff09;&#xff0c;基本就没半毛钱关系…

python脚本代码大全_python下10个简单实例代码

注意&#xff1a;我用的python2.7&#xff0c;大家如果用Python3.0以上的版本&#xff0c;请记得在print()函数哦&#xff01;如果因为版本问题评论的&#xff0c;不做回复哦&#xff01;&#xff01;&#xff01; 1.题目&#xff1a;有1、2、3、4个数字&#xff0c;能组成多少…

SpringMVC中出现 400 Bad Request 错误(用@ResponseBody处理ajax传过来的json数据转成bean)的解决方法

今天开发过程中&#xff0c;在SpringMVC中的Action中处理前台ajax请求传过来的json数据直接转成对应的实体类时出错:400 Bad Request&#xff0c;后台也不报错&#xff0c;400指的的是请求无效&#xff08;请求有语法问题或者不能满足请求&#xff09;&#xff0c;调试了好长时…