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
调用时,它失败
以下是我的项目结构:
有人能告诉我可能的原因吗
# 1 楼答案
DatabaseBuilder
的.open
方法需要打开一个现有的格式良好的Access数据库文件。java.io.File
的.createTempFile
方法创建一个0字节的文件。那么,代码呢将导致Jackcess抛出
当它试图做
DatabaseBuilder.open(dbFile)
相反,您应该
DatabaseBuilder.create
将0字节的文件转换为真正的Access数据库文件,如下所示