有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaJackcessDatabaseBuilder。开放式失败

我在我的Eclipse插件项目中使用jackcessapi。我在resources/lib下添加了jackcess-2.1.0.jar文件。我将jar包含在我的二进制构建和build.properties中。我使用连接字符串成功建立了连接,但我的DatabaseBuilder.open()调用未执行。我的代码是

 public void run() {
    try {
        File tempTarget = File.createTempFile("eap-mirror", "eap");
        try {
            this.source = DriverManager.getConnection(EaDbStringParser.eaDbStringToJdbc(sourceString));
            this.source.setReadOnly(true);

            try {
                FileUtils.copyFile(new File(templateFileString), tempTarget);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Changes
            try {
                this.target = DatabaseBuilder.open(tempTarget);
            } catch (IOException e) {
                e.printStackTrace();
            }

            Collection<String> tables = selectTables(source);
            long time = System.currentTimeMillis();
            for (String tableName : tables) {
                long tTime = System.currentTimeMillis();
                Table table = target.getTable(tableName);
                System.out.print("Mirroring table " + tableName + "...");
                table.setOverrideAutonumber(true);

                copyTable(table, source, target);
                System.out.println(" took "+ (System.currentTimeMillis() - tTime));
            }
            System.out.println("Done. Overall time: "+ (System.currentTimeMillis() - time));
            System.out.println("done");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

          // More Code here

  } catch (IOException e1) {

    }
}

当我在调试模式下运行该类并到达DatabaseBuilder.open调用时,它失败

以下是我的项目结构:

My project structure

有人能告诉我可能的原因吗


共 (1) 个答案

  1. # 1 楼答案

    DatabaseBuilder.open方法需要打开一个现有的格式良好的Access数据库文件。java.io.File.createTempFile方法创建一个0字节的文件。那么,代码呢

    File dbFile;
    try {
        dbFile = File.createTempFile("eap-mirror", "eap");
        try (Database db = DatabaseBuilder.open(dbFile)) {
            System.out.println(db.getFileFormat());
        } catch (IOException ioe) {
            ioe.printStackTrace(System.out);
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
    } finally {
        System.out.println("Finally...");
    }
    

    将导致Jackcess抛出

    java.io.IOException: Empty database file

    当它试图做DatabaseBuilder.open(dbFile)

    相反,您应该DatabaseBuilder.create将0字节的文件转换为真正的Access数据库文件,如下所示

    File dbFile;
    try {
        dbFile = File.createTempFile("eap-mirror", ".accdb");
        dbFile.deleteOnExit();
        try (Database db = DatabaseBuilder.create(Database.FileFormat.V2010, dbFile)) {
            System.out.println(db.getFileFormat());
        } catch (IOException ioe) {
            ioe.printStackTrace(System.out);
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
    } finally {
        System.out.println("Finally...");
    }