Hi Michael,
I'm not sure I understand the problem you're wrestling with. Forgive me
if I have garbled your message. It sounds as though you want to be able
to create an empty database from a template database. This is pretty
easy to do with Derby:
1) Nest another jar file inside your application jar file. The nested
jar file contains the template database.
2) At runtime, unjar your template database to the directory identified
by the Derby startup property derby.system.home. If you didn't set this
property when starting Derby, then it defaults to be the directory in
which you started the VM.
3) Rename the unjarred subdirectory to be the name you want to use for
the new database.
That should do it.
I hope this addresses your question.
Cheers,
-Rick
Michael Vinca wrote:
Hello,
I wasn't sure if this was going to work or not. There was nothing in
the documentation to suggest it would, I was just hopeful. I tried
searching the archives for "createFrom" but only got one hit, so I
hope this is not a repeat question.
I am attempting to create a writable database from a read only
database in a JAR file. The idea is to have a clickable JAR file that
can determine if an external database exists, and if not, creates it
from an original (not blank) database stored within the JAR file. A
code snippet follows:
// Connect to the database
try
{
Class.forName( "org.apache.derby.jdbc.EmbeddedDriver" ).newInstance();
//Start up derby
}
catch( Exception e )
{
e.printStackTrace();
throw new RuntimeException( e );
}
String databaseUri =
PreferenceController.Instance().getPreferenceDirectory().getAbsolutePath()
+
java.io.File.separatorChar + "vEat";
try
{
// Check to see if the database exists
java.io.File database = new java.io.File( databaseUri );
if( !database.exists() )
{
databaseUri = "jdbc:derby:" + databaseUri;
System.out.println( "Attempting to create database: " + databaseUri );
// Database does not exist. Copy the default to the preference directory
Connection conn = DriverManager.getConnection( databaseUri +
";createFrom=/org/vinca/vEat/database/vEat" );
conn.close();
}
}
catch( SQLException e )
{
// Couldn't access the database.
(Please excuse my poorly named databaseUri variable...)
The JAR file does include the database, I checked.
I get the following output (some stack removed):
Attempting to create database:
jdbc:derby:/Users/michaelj/Library/Preferences/vEat/vEat
SQL Exception: Failed to start database
'/Users/michaelj/Library/Preferences/vEat/vEat', see the next
exception for details.
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
…
ERROR XBM0Y: Backup database directory /org/vinca/vEat/database/vEat
not found. Please make sure that the specified backup path is right.
at org.apache.derby.iapi.error.StandardException.newException(Unknown
Source)
at
org.apache.derby.impl.services.monitor.PersistentServiceImpl.recreateServiceRoot(Unknown
Source)
…
Exception in thread "main" java.lang.RuntimeException: SQL Exception:
Backup database directory /org/vinca/vEat/database/vEat not found.
Please make sure that the specified backup path is right.
at org.vinca.vEat.controller.IdMapper.<init>(IdMapper.java:62)
…
Caused by: SQL Exception: Backup database directory
/org/vinca/vEat/database/vEat not found. Please make sure that the
specified backup path is right.
…
So I assume that my attempt is not supported by Derby. Can anyone
confirm? Also, anyone have any alternate suggestions on how to
accomplish creating a single JAR that can use a pre-made, but
writable, database?
Thank you,
Mike Vinca