我得到的这个例外是什么并帮助我? [重复]

问题描述 投票:-3回答:2

这个问题在这里已有答案:

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

}

输出:

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
java jdbc properties-file
2个回答
0
投票

就像@Carlos和@ PM77-1一样,答案是直截了当的“找不到文件”,简单来说,

FileInputStream fis = new FileInputStream(“db.properties”);

上面的行在java代码的同一文件夹中搜索文件db.properties但找不到它。

解决方案:您可能必须移动属性文件,或者必须提及绝对/相对路径

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


0
投票

您尝试打开的文件不存在。下面的行是您遇到问题的原因。很可能该文件位于另一个文件夹中或具有不同的名称,所以我先检查一下。

FileInputStream fis = new FileInputStream("db.properties");
© www.soinside.com 2019 - 2024. All rights reserved.