【问题标题】:java.io.FileNotFoundException: .. (No such file or directory) when retrieving DAOsjava.io.FileNotFoundException: .. (没有这样的文件或目录) 检索 DAO 时
【发布时间】:2015-11-17 20:23:00
【问题描述】:

项目明天到期,如果有人能帮我解决这个问题,那就太好了。尝试访问 derby 数据库中的 DAO 时出现以下错误。就我而言,我有一个名为 BandDAO 的类,我试图检索存储在数据库中的信息,例如 Band_Name 等

错误: java.io.FileNotFoundException: sql\createdb.txt (没有这样的文件或目录) java.io.FileNotFoundException: sql\insertdata.sql (没有这样的文件或目录)

我认为问题出在这个班级的某个地方。我在一个论坛上阅读以将 \\ 更改为 / ,因为我正在使用 Mac。虽然它消除了上述错误,但它产生了许多不同的错误。有任何想法吗?并感谢

public class SetupDb {

Logger logger = Logger.getLogger(DBManager.class.getName());

void createTables() {

    DBManager dmbgr = new DBManager();

    Connection con = dmbgr.getConnection();

    executeSqlScript(con, new File("sql\\createdb.txt"));
}

void insertSetupData() {

    DBManager dmbgr = new DBManager();

    Connection con = dmbgr.getConnection();


    executeSqlScript(con, new File("sql\\insertdata.sql"));
}

public void showData() {

    Statement stmt;

    DBManager dmbgr = new DBManager();

    Connection con = dmbgr.getConnection();

    try {
        stmt = con.createStatement();
        ResultSet results = stmt.executeQuery("select * from USERDATA");

        System.out.println("\n-----------------------------------");

        while (results.next()) {
            int id = results.getInt(1);
            String userName = results.getString(2);
            String fName = results.getString(3);
            String lName = results.getString(4);
            logger.info(id + "\t\t" + userName + "\t\t" + fName + "\t\t" + lName);
        }
        results.close();
        stmt.close();
    } catch (SQLException sqlExcept) {
        logger.log(Level.SEVERE, null,sqlExcept);
    }

}

public void executeSqlScript(Connection conn, File inputFile) {

    // Delimiter
    String delimiter = ";";

    // Create scanner
    Scanner scanner;
    try {
        scanner = new Scanner(inputFile).useDelimiter(delimiter);
    } catch (FileNotFoundException e1) {
        logger.log(Level.SEVERE, null, e1);
        return;
    }

    // Loop through the SQL file statements 
    Statement currentStatement = null;
    while (scanner.hasNext()) {

        // Get statement 
        String rawStatement = scanner.next();
        try {
            // Execute statement
            currentStatement = conn.createStatement();
            currentStatement.execute(rawStatement);
        } catch (SQLException e) {
            logger.log(Level.SEVERE, null, e);
        } finally {
            // Release resources
            if (currentStatement != null) {
                try {
                    currentStatement.close();
                } catch (SQLException e) {
                    logger.log(Level.SEVERE, null, e);;
                }
            }
            currentStatement = null;
        }
    }    

【问题讨论】:

  • 您的问题就在您打印的异常中。 FileNotFoundException。这意味着您的项目没有找到所需的文件,即 sql\createddb.txt 您可能需要检查找到文件的方式。文件实际存储在哪里?
  • 我的项目存储在我的桌面上并命名为 Gigs。在该文件夹中,我有一个包含我的 createdb.text 文件的 sql 文件夹。另外我正在使用 netbeans,所以在我的 Gigs 项目的文件选项卡中,您可以看到文件夹 sql,其中是我的 createdb.text 文件

标签: java database derby dao


【解决方案1】:

实际上很容易获得您的桌面,我认为这是您的问题所在。从我所见,您实际上从未将代码指向您的桌面。我建议不要使用上面的答案,因为例如您更改了用户名或您现在再次指向错误路径的内容。我建议您通过以下方式获取您的主路径: System.getProperty("user.home") 从那里您可以指向任何地方。这也将允许您在不同的操作系统版本上使用您的代码。我在 linux 上对此进行了测试,但 Windows 的主体是相同的。

 public static void main(String[] args) throws FileNotFoundException, IOException {
    String filePath = System.getProperty("user.home") + File.separator + "Desktop" + File.separator + "Gigs"
            + File.separator + "Sql" + File.separator + "createdb.txt";
    System.out.println(filePath);

    //now I can open that file based on the path. 
    File file = new File(filePath);

    if (file.exists()) {
        BufferedReader br = new BufferedReader(new FileReader(file));

        String line = "";

        while ((line = br.readLine()) != null) {
            System.out.println("This is text from file: " + line);
        }
    } else {
        System.err.println("No File Found");
    }
}

然后我的输出显示为:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-25
    • 1970-01-01
    • 1970-01-01
    • 2019-10-24
    • 1970-01-01
    相关资源
    最近更新 更多