我在Eclipse插件项目中使用Jackcess。我在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调用时,它将失败。
以下是我的项目结构:

有人能告诉我可能的原因吗?
发布于 2015-04-24 12:24:20
.open方法DatabaseBuilder期望打开现有格式良好的Access数据库文件。.createTempFile方法java.io.File创建一个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:空数据库文件
当它尝试做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...");
}https://stackoverflow.com/questions/29844258
复制相似问题