2015-06-03 10 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 –