Java DB(也称为Derby)不会从备份还原数据库

时间:2013-03-12 13:03:03

标签: java sql derby database-restore javadb

正如我从apache manuals读到的那样,我尝试使用以下代码恢复嵌入式Java DB(AKA Derby)数据库:

private void restoreBackup() {
    FileDialog fileDialog = new FileDialog(new Frame(), "Choose A Backup Folder)", FileDialog.LOAD);
    //fileDialog.setDirectory("::myVolume:");
    fileDialog.setVisible(true);
    String fileName = fileDialog.getDirectory();

    if (fileDialog.getDirectory() != null) {
        fileName = fileName.substring(0, fileName.lastIndexOf(System.getProperty("file.separator")));
        try {
            Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
            Properties connectionProps = new Properties();
            connectionProps.put("restoreFrom", fileName);
            connectionProps.put("user", "pass");
            connectionProps.put("password", "databaseName");
            conn = DriverManager.getConnection(dbURL, connectionProps);
            conn.commit();
            System.out.println("Database Restored");
        } catch (InstantiationException ex) {
            Logger.getLogger(ReseachAssistantUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ReseachAssistantUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ReseachAssistantUI.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ReseachAssistantUI.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

但是,即使没有抛出异常,也没有任何反应。我在这里做错了吗?

1 个答案:

答案 0 :(得分:0)

我有同样的问题,我解决了它,首先关闭数据库并恢复它。 例如:

        String nsURL1 = "jdbc:derby:*PATH*;shutdown=true";
        String nsURL2 = "jdbc:derby:*PATH*;restoreFrom="+ fileName;
        connectionProps.setProperty("user", "pass");
        connectionProps.setProperty("password", "databaseName");

        DriverManager.getConnection(nsURL1, props);
        dbConnection = DriverManager.getConnection(nsURL2, props);

并使用try-catch