2015-06-03 41 views
-1

我想通過使用config .properties文件中的值連接到數據庫。每次它給文件沒有發現異常。相同的代碼在覈心Java中起作用。如何讀取servlet中.property文件的值?

Properties prop = new Properties(); 
    InputStream input = null; 

    try { 

     input = new FileInputStream("prop/config.properties"); 

     // load a properties file 
     prop.load(input); 


     String ipaddress=prop.getProperty("com.mysql.ipaddress"); 
     String portno=prop.getProperty("com.mysql.portno"); 
     String dbname=prop.getProperty("com.mysql.dbname"); 
     String user=prop.getProperty("com.mysql.user"); 
     String password=prop.getProperty("com.mysql.password"); 

     // get the property value and print it out 

     System.out.println(ipaddress); 
     System.out.println(portno); 
     System.out.println(dbname); 
     System.out.println(user); 
     System.out.println(password); 

     try {Class.forName("com.mysql.jdbc.Driver");} 
      catch (ClassNotFoundException e) {e.printStackTrace();} 
      try 
      { 

       Connection connection2 = DriverManager.getConnection("jdbc:mysql://"+ipaddress+":"+portno+"/"+dbname,user,password); 

      Statement stmt1=connection2.createStatement(); 
      ResultSet resultset1=stmt1.executeQuery("SELECT * FROM main_countt order by 1 desc"); 

      while(resultset1.next()) { 

       System.out.println("Hello"); 
       System.out.print("<option id='"+resultset1.getString(1)+"' >"+resultset1.getString(2)+"</option>");   
         } 

      } 
      catch(Exception e) 
      { 
       e.printStackTrace(); 
      } 

    } catch (IOException ex) { 
     ex.printStackTrace(); 
    } finally { 
     if (input != null) { 
      try { 
       input.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 

例外的是我得到:

java.io.FileNotFoundException: prop\config.properties (The system cannot find the path specified) 
    at java.io.FileInputStream.open(Native Method) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at java.io.FileInputStream.<init>(Unknown Source) 
    at Test.servlet1.service(servlet1.java:44) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100) 
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166) 
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
    at java.lang.Thread.run(Unknown Source) 

回答

-1

似乎您試圖訪問錯誤的道路。嘗試把完整路徑(絕對路徑):

c:/.../prop/conf... 
+0

相同的代碼與核心java和絕對路徑一起工作。 我想要的是讀取servlet中的屬性文件值。我不能使用絕對路徑,因爲我將在稍後將其部署到服務器上。 –

+0

什麼是你的文件的絕對路徑? –

+1

感謝您的重播。 我從上面提供的鏈接中得到了答案。 我已經使用了下面的代碼和它的工作。 (Thread.currentThread()。getContextClassLoader()。getResourceAsStream(「prop/config.properties」)); 文件位置: WEB-INF/classes/prop/config.properties –