当前位置:Gxlcms > mysql > jdbc-JDBC使用反射读取properties文件出错

jdbc-JDBC使用反射读取properties文件出错

时间:2021-07-01 10:21:17 帮助过:56人阅读

jdbcjavajava反射mysql

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

  1. <code>@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(); }</code>

图片说明
图片说明
图片说明
图片说明

回复内容:

读取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中的,否则会扫描普通文件,希望对你理解有帮助

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

1,把jdbc.properties 放到src下 ,
2.

  1. <code> public class CMConstant {
  2. public static String getConfigureParameterFromJDBC(String paramString) {
  3. String str = CMConstant.getRootPath() + File.separator + "WEB-INF"
  4. + File.separator + "classes/config.properties";
  5. Properties localProperties = new Properties();
  6. try {
  7. FileInputStream localFileInputStream = new FileInputStream(str);
  8. localProperties.load(localFileInputStream);
  9. localFileInputStream.close();
  10. } catch (FileNotFoundException localFileNotFoundException) {
  11. logger.error("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
  12. localFileNotFoundException.printStackTrace();
  13. return null;
  14. } catch (IOException localIOException) {
  15. logger.error("装载文件--->失败!");
  16. localIOException.printStackTrace();
  17. }
  18. return localProperties.getProperty(paramString);
  19. }
  20. public static String getRootPath() {
  21. String result = null;
  22. try {
  23. result = CMConstant.class.getResource("CMConstant.class").toURI()
  24. .getPath().toString();
  25. } catch (URISyntaxException e1) {
  26. e1.printStackTrace();
  27. }
  28. int index = result.indexOf("WEB-INF");
  29. if (index == -1) {
  30. index = result.indexOf("bin");
  31. }
  32. result = result.substring(1, index);
  33. if (result.endsWith("/"))
  34. result = result.substring(0, result.length() - 1);// 不包含最后的"/"
  35. return result;
  36. }
  37. /**
  38. * 查找config/jdbc.properties里值
  39. * @Description: @param key
  40. * @Description: @return
  41. * @Last Modified: , Date Modified:
  42. */
  43. public static String getJDBCValue(String key) {
  44. String filePath = getRootPath() + File.separator + "WEB-INF\\classes"
  45. + File.separator + "config.properties";
  46. Properties propertie = new Properties();
  47. try {
  48. FileInputStream inputFile = new FileInputStream(filePath);
  49. propertie.load(inputFile);
  50. inputFile.close();
  51. } catch (FileNotFoundException ex) {
  52. ex.printStackTrace();
  53. return null;
  54. } catch (IOException ex) {
  55. ex.printStackTrace();
  56. }
  57. return propertie.getProperty(key);
  58. }
  59. }
  60. </code>

人气教程排行