检索DAO时java.io.FileNotFoundException:..(没有这样的文件或目录)

时间:2015-11-17 20:23:01

标签: java database derby dao

明天的项目到期,如果有人可以帮助我,那将是很好的。尝试访问我的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;
        }
    }    

1 个答案:

答案 0 :(得分:0)

实际上很容易获得您的桌面,我认为这是您的问题所在。您实际上从未将我的代码指向您的桌面。我建议不要回答上面的答案,因为比如说你改变了你的用户名,或者你现在再次指向错误的路径。我建议通过以下方式获取您的主路径:System.getProperty(“user.home”),您可以从那里指向任何地方。这也允许您在不同的OS版本上使用您的代码。我在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");
    }
}

我的输出显示为:

enter image description here