8

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?

2
  • This error occurs when the given database-URL is invalid. Try printing out the string you give to getConnection() Commented Mar 3, 2013 at 15:10
  • I printed it already and it's the exact path to the sqlite DB. Commented Mar 3, 2013 at 15:12

4 Answers 4

15

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

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

5 Comments

I read that using jdbc:sqlite::resource: the db is read only.
If your database (like mine) is simply in the resources directory of your project, then the following connection string will suffice: jdbc:sqlite::resource:test.sqlite or jdbc:sqlite::resource:test.db depending on your version of SQLite. I spent way too much time trying to figure this out. the resource addition to the URI is what got it working, because without it, the driver assumes your db is in the same directory as the file that is connecting to it.
I follow this and got an exception java.sql.SQLException: resource quanlythuchi3.sqlite not found: java.net.MalformedURLException: no protocol: MyDB.sqlite
@Chris Cirefice thanks a lot for your comment. Your suggestion worked for me.
Thanks a ton @Chris Cirefice .. I was facing exactly the same exception and your comment made my day... :)
2

The ::resource way is right. And these explanations will help if you are using ::resource but still get errors like resource database.db not found: java.net.MalformedURLException: no protocol: database.db like verdana.

Most common anwsers are: DriverManager.getConnection("jdbc:sqlite::resource:path/to/database.db")

However the path/to/database.db must be the exact path(the Path in Real File System but not in Jar) to your jar file.

I recommend to use getClass().getResource():

DriverManager.getConnection("jdbc:sqlite::resource:" + 
        getClass().getResource("/path/to/db/in/the/jar/file"))

NOTE:the /path/to/db/in/the/jar/file part must start with /

Comments

0

I'm not sure if this has changed in recent JDBC versions, but after fiddling with it for about an hour and getting various exceptions (MalformedURLException and "Database has been closed"), I found that the following worked for me:

DriverManager.getConnection("jdbc:sqlite::resource:file:/path/to/database.db")

The :file: portion seems to be missing from other answers and I couldn't get it to work without it.

Also, note that the

/path/to/database.db

is the absolute path starting from the root of the .jar file, rather than a normal resource root.

Comments

0
jdbc:sqlite::resource:notes_app.db

worked for me. My database(notes_app.db) was in the root of generated jar file.

public class Database {
    public static Connection con () throws SQLException {
        String url = "jdbc:sqlite::resource:notes_app.db";
        return DriverManager.getConnection(url);
    }
}

My notes_app.db is in the root of generated jar. I'm developing with maven and notepadd++

Comments

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.