tags:

views:

69

answers:

1

Hi,

I'm creating a program in Java in which I have to use a lot of tables. I made the decision to create all those tables in a JavaDB file.

However, now I want to distribute this filled JavaDB file with my JAR file for distribution, since connecting to a server is not an option.

In the past I could only distribute an empty database with the JAR by using that Derby package.

Any ideas?

Thank you so much!

+1  A: 

I'm not sure I understood the question but it is possible to package a read-only database inside a JAR. From the derby documentation:

Accessing Databases-in-a-Jar in the Class Path

Once an archive containing one or more Derby databases has been created it can be placed in the class path. This allows access to a database from within an application without the application's knowing the path of the archive. When jar or zip files are part of the class path, you do not have to specify the jar subsubprotocol to connect to them.

To access a database in a zip or jar file in the class path:

  1. Set the class path to include the jar or zip file before starting up Derby:

    CLASSPATH="C:\dbs.jar;%CLASSPATH%"
    
  2. Connect to a database within the jar or zip file with one of the following connection URLs:

    jdbc:derby:/databasePathWithinArchive
    
    
    (standard syntax)
    
    
    jdbc:derby:classpath:/databasePathWithinArchive
    
    
    (syntax with subsubprotocol)
    

For example:

jdbc:derby:/products/boiledfood
jdbc:derby:classpath:/products/boiledfood

If this doesn't answer the question, please clarify.

Pascal Thivent
Thanks Pascal, I think i'm a beginner at this..Where do I place the Database file in the JAR?and what is a subprotocol / classpath?Thanks for helping!Really appreciate it!
Paintrick
@Paintrick: 1. The `databasePathWithinArchive` is relative path to root of classpath elements. 2. the subprotocol is this part: `jdbc:derby:[subprotocol]`
Pascal Thivent