视频1 视频21 视频41 视频61 视频文章1 视频文章21 视频文章41 视频文章61 推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37 推荐39 推荐41 推荐43 推荐45 推荐47 推荐49 关键词1 关键词101 关键词201 关键词301 关键词401 关键词501 关键词601 关键词701 关键词801 关键词901 关键词1001 关键词1101 关键词1201 关键词1301 关键词1401 关键词1501 关键词1601 关键词1701 关键词1801 关键词1901 视频扩展1 视频扩展6 视频扩展11 视频扩展16 文章1 文章201 文章401 文章601 文章801 文章1001 资讯1 资讯501 资讯1001 资讯1501 标签1 标签501 标签1001 关键词1 关键词501 关键词1001 关键词1501 专题2001
jdbc-JDBC使用反射读取properties文件出错
2020-11-09 09:19:26 责编:小采
文档
 jdbcjavajava反射mysql

使用反射获取类加载器来读取properties文件出现空指针异常,可以使用直接使用输入流读取properties文件,为什么教学视频中却可以使用反射?另外问下怎么在PC端提问,不是论坛发帖,单纯悬赏C币提问,我都是在手机上提,电脑上修改

@Test public void getConnection() throws Exception { /* * 读取配置文件来获取数据库连接 */ Properties properties = new Properties(); String driverClass = null; String jdbcUrl = null; String user = null; String password = null; InputStream in = this.getClass().getClassLoader().getResourceAsStream("C:/Java/WprkSpace/JDBC/jdbc.properties"); properties.load(in); driverClass = properties.getProperty("driver"); jdbcUrl = properties.getProperty("jdbcUrl"); user = properties.getProperty("user"); password = properties.getProperty("password"); Driver driver = (Driver) Class.forName(driverClass).newInstance(); properties.put("user", user); properties.put("password", password); Connection connerction = driver.connect(jdbcUrl, properties); System.out.println( connerction); in.close(); }




回复内容:

读取properties文件可以有多种方式,用IO或者用ClassLoader,绝对路径的用IO,相对路径可以用IO也可以用ClassLoader,因为相对路径是相对于Classpath的。一般来说用到ClassLoader的话都是用的相对路径了。

http://bbs.csdn.net/topics/391984276

使用PropertyPlaceholderConfigurer读取.properties文件(1)
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

路径改成相对于SRC的路径吧。

classloader去加载的是classpath下的资源,加*会加载jar中的,否则会扫描普通文件,希望对你理解有帮助

 一般没有绝对路径这种用法吧,非要用试一试
 Properties props = new Properties(); 
 File file=new File("H:\\DESKTOP\\email.properties");
 props.load(this.getClass().getClassLoader().getResourceAsStream(file.getName()));

1,把jdbc.properties 放到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;
 }
/**
 * 查找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);
 }
}

下载本文
显示全文
专题