-1

I am trying to connect to database by using values from config .properties file. Every time it gives file not found exception. Same code works in core java.

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();
            }
        }
    }

Exception that i am getting:

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)
Gyrocode.com
  • 51,125
  • 13
  • 124
  • 164
Anmol Naik
  • 26
  • 1
  • 4

1 Answers1

-1

It seems that you are trying to access to the wrong path. Try to put full path (absolute path):

c:/.../prop/conf...
Francisco Romero
  • 11,900
  • 18
  • 71
  • 151
  • Same code works with core java and also with absolute path . What i want is to read property file values in servlet. i cant use absolute path because i am going to deploy it on server later. – Anmol Naik Jun 04 '15 at 14:48
  • What is the absolute path of your file? – Francisco Romero Jun 04 '15 at 14:52
  • 1
    Thanks for the replay. I got the answer from the link that provided above. I have used below code and its working. prop.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("prop/config.properties")); File location : WEB-INF/classes/prop/config.properties – Anmol Naik Jun 04 '15 at 15:41
  • I'm glad that you solved your problem. Please put your solution as an answer to let another people see the problem and know that the question it's solved. Thanks for post your solution! – Francisco Romero Jun 04 '15 at 16:11