Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add sqlite DB to executable JAR file

Tags:

java

eclipse

jdbc

I am using JAVA (with eclipse juno) and try to create an executable JAR file which include sqlite DB file. I've try to get connection to the DB by this line:

DriverManager.getConnection("jdbc:sqlite:"+DataController.class.getResource("test.sqlite").getPath())

The DataController is a class that located where the sqlite located.

and i keep get an error:

java.sql.SQLException: invalid database address

Does someone can help and give step by step instructions about how to include sqlite DB inside an executable JAR file?

like image 311
gran33 Avatar asked Mar 03 '13 15:03

gran33


1 Answers

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.-

like image 158
jabbie Avatar answered Sep 28 '22 18:09

jabbie