9

I'm using H2 embedded database for my application. I would like to contain everything the application needs in it's own Jar, including it's database if possible. My app does not need to create temp files or anything, so basically the user just runs the Jar.

Is it possible to embed a database inside a Jar, and be able to INSERT new records as well as just SELECT out?

EDIT: Just to clarify, I'm not looking to embed the H2 driver jar inside my distributable jar, I'm looking to embed the h2 database file (someDatabase.h2.db file) inside a Jar and still be able to write/read from that database.

1 Answer 1

11

If you wish to embed the myDatabase.h2.db file inside the .jar, you can do so, but you'll have read-only access to the database. As .jar files are read-only, you can't modify them and therefore can't execute INSERT, DELETE or any DDL command.

That being said, below is an explanation on how to embed it read-only.

According to H2's documentation:

The JDBC URL "jdbc:h2:~/myDatabase" tells the H2 Engine to look for a database file named myDatabase.h2.db in the home directory of the current user.

The JDBC URL "jdbc:h2:file:/myDatabase" tells the H2 Engine to look for a database file named myDatabase.h2.db in the current directory (where the java program was executed).

If you embed the h2.db file inside a .jar, it is not accessible in a plain way. It is only accessible as a file inside a zip file.

In order to make H2 uset it, you have to use a zip as URL:

jdbc:h2:zip:~/data.zip!/test

See more in "Read Only Databases in Zip or Jar File".

When you embed the file as a resource in the jar, you may get it's relative url. Using...

MyClass.class.getClassLoader().getResource("myDatabase.h2.db")

...you'll get something like:

jar:file:/C:/folder1/folder2/myJar.jar!/myDatabase.h2.db

You can then manipulate it as a String and pass as JDBC URL connection to H2.

Sign up to request clarification or add additional context in comments.

8 Comments

see my Edit above. More looking to embed the database file itself (ie. someDatabase.h2.db file" inside my Jar, not the driver jar.
One thing I can already tell you: even if you could embedd the h2.db file inside the .jar, you wouldnt be able to do inserts, as the jar file is read-only...
I was afraid of that... even if the jar isn't compressed (jars are just zip's really), I wasn't sure if I could r/w to it...
Exactly. This is very common using .properties files. They use the read-only jar-embedded file until the user decides to make a change. When that happens, they create a file in a folder as you mentioned. (Or in the current jar's folder or subfolder.)
There you go. As it turns out, you do can use it as read-only. Check out the updated answer. Tell me if you need more code example.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.