Jackcess DatabaseBuilder.open失败 [英] Jackcess DatabaseBuilder.open fails

查看:209
本文介绍了Jackcess DatabaseBuilder.open失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Eclipse插件项目中使用了Jackcess API。我在资源/ lib 下添加了 jackcess-2.1.0.jar 文件。我把jar放在我的Binary构建之下,在$ code> 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();
}
//更改
try {
this.target = DatabaseBuilder.open(tempTarget);
} catch(IOException e){
e.printStackTrace();
}

集合< String> tables = selectTables(source);
long time = System.currentTimeMillis();
for(String tableName:tables){
long tTime = System.currentTimeMillis();
表table = target.getTable(tableName);
System.out.print(镜像表+ tableName +...);
table.setOverrideAutonumber(true);

copyTable(table,source,target);
System.out.println(taken+(System.currentTimeMillis() - tTime));
}
System.out.println(完成。总时间:+(System.currentTimeMillis() - 时间));
System.out.println(done);
} catch(SQLException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
} finally {

//更多代码在这里

} catch(IOException e1){

}
}

当我以调试模式运行课程,我达到 DatabaseBuilder.open 调用失败



这是我的项目结构:





有人可以告诉我可能的原因吗? / p>

解决方案

.open 方法 DatabaseBuilder 期望打开一个现有的格式良好的Access数据库文件。 java.io.File .createTempFile 方法创建一个0字节的文件。所以,代码

 文件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:空数据库文件


当尝试执行 DatabaseBuilder.open(dbFile)



相反,您应该 DatabaseBuilder.create 将0字节的文件转换成这样一个真正的Access数据库文件

 文件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 ...);
}


I am using Jackcess API in my Eclipse plugin project. I added jackcess-2.1.0.jar file under resources/lib. I included the jar under my Binary build and in build.properties. I successfully make a connection using connection string but my DatabaseBuilder.open() call is not executing. My code is

 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) {

    }
}

When I run the class in debug mode and I reach DatabaseBuilder.open call it fails.

Here is my project structure:

Can anyone tell me the possible reason for it ?

解决方案

The .open method of DatabaseBuilder expects to open an existing well-formed Access database file. The .createTempFile method of java.io.File creates a 0-byte file. So, the code

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...");
}

will cause Jackcess to throw

java.io.IOException: Empty database file

when it tries to do DatabaseBuilder.open(dbFile).

Instead, you should DatabaseBuilder.create to convert the 0-byte file into a real Access database file like this

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...");
}

这篇关于Jackcess DatabaseBuilder.open失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆