-3
package jdbc.examples;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

public class PropertiesDemo {
    private static Connection conn;
    private static Statement st;
    private static ResultSet rs;

    public static void main(String[] args) throws Exception{
        Properties p = new Properties();
        FileInputStream fis = new FileInputStream("db.properties");
        p.load(fis);
        String driver = (String)p.getProperty("driver");
        String url = (String)p.getProperty("url");
        String user = (String)p.getProperty("user");
        String pwd = (String)p.getProperty("pwd");

            Class.forName(driver);
            conn = DriverManager.getConnection(url, user, pwd);
            st = conn.createStatement();
            rs = st.executeQuery("select ename, sal, deptno from emp");

        while(rs.next()) {
            System.out.println(rs.getString(1)+"  "+rs.getDouble(2)+"  "+rs.getInt(3));
        }

        rs.close();
        st.close();
        conn.close();
    }

}

output:

Exception in thread "main" java.io.FileNotFoundException: db.properties (The system cannot find the file specified)     
at java.base/java.io.FileInputStream.open0(Native Method)   
at java.base/java.io.FileInputStream.open(Unknown Source)   
at java.base/java.io.FileInputStream.<init>(Unknown Source)     
at j
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • What? The error message says "The system cannot find the file specified" in this case "db.properties". The code says "new FileInputStream("db.properties")" - so there must be a file with that name; and `getProperty("driver")` with a key-value pair like "driver = ..." in the file – user85421 Mar 11 '19 at 18:38
  • `File Not Found` means just that. – PM 77-1 Mar 11 '19 at 18:38
  • Stackoverflow generally isn't for debugging help, please read [How To Ask](https://stackoverflow.com/help/how-to-ask) and [How to create an MCVE](https://stackoverflow.com/help/mcve) so you do not make the same mistakes in the future. – Mercury Platinum Mar 11 '19 at 18:40

2 Answers2

0

File that you're trying to open doesn't exist.The line below is the cause of your problem. Most likely the file is in another folder or has different name, so i would check that first.

FileInputStream fis = new FileInputStream("db.properties");
ferals
  • 19
  • 2
0

Like @Carlos & @PM77-1 mentioned the answer is straight forward "File is not found", In simpler term,

FileInputStream fis = new FileInputStream("db.properties");

The above line search for the file db.properties in the same folder of the java code but it is not found.

Solution : you may have to move the property file or you have to mention absolute/relative path

FileInputStream fis = new FileInputStream("c:\myFile\db.properties");

Avi
  • 1,458
  • 9
  • 14