【问题标题】:What is this Exception i got and Help me out? [duplicate]我得到了什么异常并帮助我? [复制]
【发布时间】:2019-03-11 18:34:05
【问题描述】:
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

【问题讨论】:

  • 什么?在这种情况下,错误消息显示“系统找不到指定的文件”“db.properties”。代码显示“new FileInputStream("db.properties")” - 所以必须有一个具有该名称的文件;和 getProperty("driver") 在文件中带有像“driver = ...”这样的键值对
  • File Not Found 就是这个意思。
  • Stackoverflow 一般不提供调试帮助,请阅读How To Ask 和How to create an MCVE,以免以后再犯同样的错误。

标签: java jdbc properties-file


【解决方案1】:

就像@Carlos 和@PM77-1 提到的那样,答案是直截了当的“找不到文件”,简而言之,

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

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

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

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 我将该文件保存在同一个文件夹中并使用相同的名称
    • 尝试使用文件的绝对路径来检查问题是否仍然存在。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2018-06-22
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多