2015-04-24 32 views
1

我在Eclipse插件項目中使用了Jackcess API。我在資源下添加了jackcess-2.1.0.jar文件/ lib。我在我的二進制版本和build.properties下包含了這個jar。我成功地使用連接字符串進行連接,但是我的DatabaseBuilder.open()調用未執行。我的代碼是Jackcess 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

誰能告訴我它可能的原因是什麼?

+0

異常的真正Access數據庫文件被拋出,你就吞嚥下去? – Kayaman

+0

沒有異常拋出我正在捕捉異常但沒有拋出異常我認爲 – wearybands

+0

Jackcess 1.2.6是一個非常舊的版本。爲什麼不使用[當前版本](http://sourceforge.net/projects/jackcess/files/latest/download?source=files)有沒有特別的原因? –

回答

0

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:空數據庫文件

當它試圖做 DatabaseBuilder.open(dbFile)

相反,你應該DatabaseBuilder.create到0字節的文件轉換成這樣

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