簡體   English   中英

檢索 DAO 時出現 java.io.FileNotFoundException: .. (沒有這樣的文件或目錄)

[英]java.io.FileNotFoundException: .. (No such file or directory) when retrieving DAOs

項目明天到期,所以如果有人能幫我解決這個問題就太好了。 嘗試訪問我的 derby 數據庫中的 DAO 時,我收到以下錯誤。 就我而言,我有一個名為 BandDAO 的類,我正在嘗試檢索存儲在數據庫中的信息,例如 Band_Name 等

錯誤:java.io.FileNotFoundException: sql\\createdb.txt (No such file or directory) java.io.FileNotFoundException: sql\\insertdata.sql (No such file or directory)

我認為問題出在這堂課的某個地方。 我在一個論壇上閱讀以將 \\\\ 更改為 / 因為我使用的是 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;
        }
    }    

獲取桌面實際上很容易,我認為這是您的問題的來源。 從我所看到的,你從來沒有真正將你的代碼指向你的桌面。 我建議不要使用上面的答案,因為例如您更改了用戶名或您現在再次指向錯誤路徑的內容。 我建議通過以下方式獲取您的主路徑: 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");
    }
}

我的輸出然后顯示為:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM