因此,這裏是我的簡單的設置,在這裏我設置必要的臨時目錄屬性。
private static void accessReadOnlyJar() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
Properties props = new Properties();
String tempDir = System.getProperty("java.io.tmpdir");
props.setProperty("derby.storage.tempDirectory", tempDir);
props.setProperty("derby.stream.error.file", tempDir+"derby_error.log");
Connection connection = DriverManager.getConnection("jdbc:derby:jar:(data/db.jar)derbyDB", props);
Statement statement = connection.createStatement();
ResultSet result = statement.executeQuery("SELECT * FROM TESTTABLE");
while(result.next()){
System.out.println(result.getString("NAME"));
}
connection.close();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
您的tempDir良好的位置可能是:
- 的系統臨時目錄:
System.getProperty("java.io.tmpdir")
- 你的工作目錄:
"."
- 您的應用程序目錄:
ClassLoader.getSystemClassLoader().getResource(".").getPath()
或任何相對於它;-)
如果這不能解決您的問題,請檢查/重新創建您的數據庫。如果在打包jar之前它沒有關閉 ,德比將始終在啓動時嘗試恢復。
您是否已將「derby.storage.tempDirectory」和「derby.stream.error.file」屬性設置爲可寫入的臨時目錄? – Sascha
@Sascha可能是它..我如何將目錄設置在jar文件的外部?我希望這可以在Windows和Mac上工作,所以我不想把它放在C://或/ usr之類的地方或類似的地方。 – Mukhi