jdbc-JDBC使用反射讀取properties文件出錯(cuò)

jdbcjavajava反射mysql

使用反射獲取類加載器來讀取properties文件出現(xiàn)空指針異常,可以使用直接使用輸入流讀取properties文件,為什么教學(xué)視頻中卻可以使用反射?另外問下怎么在pc端提問,不是論壇發(fā)帖,單純懸賞c幣提問,我都是在手機(jī)上提,電腦上修改

@Test public   void getConnection() throws Exception { /* * 讀取配置文件來獲取數(shù)據(jù)庫(kù)連接 */    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(); }

jdbc-JDBC使用反射讀取properties文件出錯(cuò)
jdbc-JDBC使用反射讀取properties文件出錯(cuò)
jdbc-JDBC使用反射讀取properties文件出錯(cuò)
jdbc-JDBC使用反射讀取properties文件出錯(cuò)

回復(fù)內(nèi)容:

讀取properties文件可以有多種方式,用IO或者用ClassLoader,絕對(duì)路徑的用IO,相對(duì)路徑可以用IO也可以用ClassLoader,因?yàn)橄鄬?duì)路徑是相對(duì)于Classpath的。一般來說用到ClassLoader的話都是用的相對(duì)路徑了。

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

使用PropertyPlaceholderConfigurer讀取.properties文件(1)
———————-biu~biu~biu~~~在下問答機(jī)器人小D,這是我依靠自己的聰明才智給出的答案,如果不正確,你來咬我啊!

路徑改成相對(duì)于SRC的路徑吧。

classloader去加載的是classpath下的資源,加*會(huì)加載jar中的,否則會(huì)掃描普通文件,希望對(duì)你理解有幫助

        一般沒有絕對(duì)路徑這種用法吧,非要用試一試         Properties props = new Properties();          File file=new File("H:DESKTOPemail.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("讀取屬性文件--->失??!- 原因:文件路徑錯(cuò)誤或者文件不存在");             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-INFclasses"                 + 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);     } } 
? 版權(quán)聲明
THE END
喜歡就支持一下吧
點(diǎn)贊10 分享