Java Quiz Player

Blog Post

Restoring Java DB Database from a Backup Copy

Apr 27, 2013

This blog post is about restoring Java DB database from a backup copy.

1. Java DB

Java DB is a relational database management system that is based on the Java programming language and SQL. This is the Oracle release of the Apache Software Foundation's open source Derby project. Java DB is included in the Java SE 7 SDK.

Java DB provides a way to backup a database and restore it from the backup copy.

A Java DB database is stored in files within a directory with the same name as that of the database. The backup database is stored at a specified location. The database can be restored from the backup copy at a specified location.

2. Restoring the Database

Restoring a database deletes the existing one, copies the backup copy and starts the restored (copied) database. In case there is no existing database, a new database is created and the backup is copied.

Restore process steps:

The following is an example Java method to restore a database from a backup copy using JDBC:

/*
 * Starts database using a backup copy. The returned Connection object
 * is used to access the restored database objects and data.
 * Note: 
 * (1) The existing database is shutdown prior to running this method.
 * (2) The connection URL uses the 'restoreFrom' attribute.
 * (3) The database driver used is for a Java DB database deployed with
 * embedded option.
 */
public static Connection restoreDatabaseRoutine()
		throws SQLException, ClassNotFoundException {

	String backupPath = "D:/BackupDir/sampleDb";
	String restoreUrl = "jdbc:derby:sampleDb;restoreFrom=" + backupPath;
	String driver = "org.apache.derby.jdbc.EmbeddedDriver";
	Class.forName(driver); // throws ClassNotFoundException
	Connection conn = DriverManager.getConnection(restoreUrl);
	return conn;
}

2.1. createFrom Attribute in URL

createFrom attribute can be used instead of the restoreFrom attribute to restore a database. In case a database with the same name exists, the restore process is aborted with an error. The database is created and restored when there is no existing database.

2.2. Restoring Database Interactively with 'ij' Tool

ij is a command line tool included with Java DB. ij is a JDBC tool used to run interactive queries on a Java DB database.

Start ij, connect to the database with the restored database:

os prompt> ij 
ij> CONNECT 'jdbc:derby:sampleDb;restoreFrom=D:/BackupDir/sampleDb';

'jdbc:derby:sampleDb;restoreFrom=D:/BackupDir/sampleDb' is the connection URL. The value of restoreFrom attribute is the directory path to the backup copy of the sampleDb database. Note that the database must be closed prior to restoring it.

3. Example Usage

An example usage is a Swing based desktop application that uses a database. The application has a function to restore database from a backup copy. The following is an outline of the function:

This database menu can also include an option to make an online backup.

4. References

Return to top


Comments

Comments are welcome. Please note that comments are moderated.
Email to: info(at)javaquizplayer(dot)com.