Restore Derby Database with Java code

I want to restore my embedded derby database using Java code. I used the code below for restoring my database. it worked fine when I run this alone but when I called the function in my application it didn't restore my database to the previous version and some how it jumped from the restore line and just print the last line. I don't know how to make it work for my project. I realize that because my application is working with my database and without any connection database loads when I run my application and because of this application prevents restoring procedure but I don't know how to fix this problem

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;


public class Restore {


    public  void restoreDatabaseRoutine(String address)
        throws SQLException, ClassNotFoundException {

    String backupPath = address;
    System.out.println(address);
    String restoreUrl = "jdbc:derby:Test;restoreFrom=" + backupPath;
    String driver = "org.apache.derby.jdbc.EmbeddedDriver";
    Class.forName(driver); // throws ClassNotFoundException
    Connection conn = DriverManager.getConnection(restoreUrl);
    conn.close();
    System.out.println("The database has been successfully restored");
    //return conn;
}
}

1 Answer

  1. Jerome- Reply

    2019-11-14

    You need the JDBC driver in your class path. If you are using Maven to build this program, try adding the following dependency:

    <!-- https://mvnbuild.com/artifact/org.apache.derby/derby -->
    <dependency>
      <groupId>org.apache.derby</groupId>
      <artifactId>derby</artifactId>
      <version>10.13.1.1</version>
    </dependency>
    

    The driver class can be found in the list of classes for the artifact: https://mvnbuild.com/artifact/org.apache.derby/derby/10.13.1.1

Leave a Reply

Your email address will not be published. Required fields are marked *

You can use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>