0

As I've read from the apache manuals, I tried to restore my embedded Java DB (AKA Derby) Database using this code:

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

But, nothing happens, even though no exceptions are thrown. Am I doing something wrong here?

Igor
  • 1,532
  • 4
  • 23
  • 44
  • Can you try using the full path in place of fileName ? Are you getting the fileName and path in the format specified in the apache manual you posted ? – happybuddha Mar 12 '13 at 13:29
  • @happybuddha I just did and it's still the same. This is the fileName I get `"C:\Users\Igor\Desktop\BackUpData2013-03-12@01-22-19\Research Assistant Data V0.26"` which is a folder actually. – Igor Mar 12 '13 at 13:38
  • Hmm... I looked at the manual again and it says '..the _connection URL_ should be: jdbc:derby:toursDB;createFrom=c:\mybackups\sample' can you alter your dbURL to look like the above ? Of course it would be restoreFrom and not createFrom – happybuddha Mar 12 '13 at 13:57
  • @happybuddha I'm sorry, I can't see what needs to be altered. What's different in your example? Here's the whole string : `jdbc:derby:Research Assistant Data V0.26;restoreFrom=C:\Users\Igor\Desktop\BackUpData2013-03-12@01-22-19\Research Assistant Data V0.26` – Igor Mar 12 '13 at 14:16
  • What happens if you try the same command in the "ij" tool? Run IJ and then 'connect jdbc:derby:dbname;restoreFrom=backupName'; – Bryan Pendleton Mar 12 '13 at 14:40
  • @BryanPendleton I'm not familiar with ij, unfortunately. – Igor Mar 12 '13 at 14:42
  • Do you have access to a command line for the database ? Through a tool may be ? Where would you run queries ? for ex. if you had to do select * from emp, where would you run this statement ? Can you go to that screen and run the command Bryan says. Alternately, can you update your code to getConnection(String DBurl) ? and make sure the URL is in the format the apache manual says it should be in. – happybuddha Mar 12 '13 at 16:24
  • @happybuddha I use SQuirrel and the the built-in database service in NetBean. I can execute that statement, it shows an error. – Igor Mar 12 '13 at 16:37
  • I also edited my comment to include a different getConnection method, give that a shot too. – happybuddha Mar 12 '13 at 16:40
  • @happybuddha I did that too, no effect whatsoever. – Igor Mar 12 '13 at 16:43
  • It's not a proper solution, but I might just delete the database folder and copy the one from the backup directory. – Igor Mar 12 '13 at 16:47
  • 1
    >it shows an error. What error does it show ? Also, did you try to put debug points and inspect at each line of code ? – happybuddha Mar 12 '13 at 16:49
  • @happybuddha SQL syntax error. I can not execute this 'connect' statement or I don't know the SQL equvallent of it. – Igor Mar 12 '13 at 16:50
  • A few things : 1) Do you have a database at all ? The doc says it will delete an existing db and create a new one. 2) Do you have admin privileges to do this operation ? 3) can you rename the back up file and try ? – happybuddha Mar 12 '13 at 18:16
  • 1
    Heres more documentation from one of Bryans earlier posts : http://db.apache.org/derby/docs/10.9/getstart/ – happybuddha Mar 12 '13 at 19:15
  • @happybuddha I do have a functional database and I do have the admin privilidge to delete and copy. I can't rename the backup file cause that would rename the database name. The backup is actually a copy of the database folder, not a file. Also the site you provided doesn't change the URL when you click on links, so your link opens the root page. I apreciate you are trying. I would like to get this to work, but since the backup is a folder, I created a method which copies this folder and replaces current database folder with it. – Igor Mar 12 '13 at 23:27

1 Answers1

0

I had the same problem and I solved it, shutting it down first the database and after restoring it. For example:

        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);

and using try-catch

Pap Kostis
  • 11
  • 1
  • 1
    Works for me. But attention: If the backup was made via CALL SYSCS_UTIL.SYSCS_BACKUP_DATABASE('BACKUPPATH') Procedure then the restoring must be with `restoreFrom=BACKUPPATH/DATABASENAME`. – mr.wolle Aug 04 '14 at 15:56