How to Include SQLite Database in Executable Jar

How to include SQLite database in executable Jar?

What library are you using for SQLite?

I did a search based on the connection URI you indicated and found this one. In the documentation it says:

2009 May 19th: sqlite-jdbc-3.6.14.1 released. This version supports "jdbc:sqlite::resource:" syntax to access read-only DB files contained in JAR archives, or external resources specified via URL, local files address etc. (see also the detailes)

If that is the driver you are using, then I would suggest the following connection URI:

"jdbc:sqlite::resource:DatabaseFile"

The key is that since your database is in a jar file, it can not be access as a file with FileInputStream. Instead it must be accessed through the JVM's support for it (namely with Class.getResource() or Class.getResourceAsStream()). Do note that resources contained within jar files are read-only. You won't be able to save any changes to your database.

Jar file cannot read the sqlite database file

I finally found a solution. I tried the method on Github sqlite-jdbc and it didn't work. It turned out that I had to manually create a resource folder and add it as a resource folder in the project settings of Intellij Idea IDE. Then I had to define the connection as

connection = DriverManager.getConnection("jdbc:sqlite::resource:test.db");

and put the db file inside the resources folder. The method of adding the resource folder is different from IDE to IDE. When the project is run, it automatically identifies the resource folder and the system will bundle the database file inside the jar file.

add sqlite DB to executable JAR file

Apparently sqlite-jdbc can open resources on it's own. Per this thread https://groups.google.com/forum/?fromgroups=#!topic/xerial/Oayzj5nrJGk, add :resource to the path. So try the following:

DriverManager.getConnection("jdbc:sqlite::resource:package/test.sqlite"); 

or depends on version of sqlite

DriverManager.getConnection("jdbc:sqlite::resource:package/test.db"); 

Replacing package with the '/' separated path to the package this file is in.

Be aware that it will actually copy the file to a tmp directory.-

How to use SQLite database inside jar file?

Another good option to consider could be derby, javadb. It's bundled with java now and is pretty fast to setup and has good performances.

Regards,
Stéphane

Is it possible to access a SQLite database file outside of an executable JAR?

Well, here is such term as "working directory". It is the directory from where your application is started. So, every "relative" path in your application is usually relative to this "working directory".

Now let's return to your problem. When you set the path to a file you can make it either relative, absolute or relative to the JAR inner directory root, depending how you do create the file and set the path.

So, just recheck how paths are set in your application and try to use the relative one, running you application from the appropriate directory.



Related Topics



Leave a reply



Submit