31

I have created a Swing application that uses SQLite as a local database. The database file is located in project's root directory.

Project/DatabaseFile

The application runs fine on Eclipse, but when I run the packaged executable Jar, I get the following error:

No such table : table1

This means that the database is not reachable. When I examined the contents of the resulting JAR file, the database file was not there anymore.

In the code, I've linked the database as follows:

jdbc:sqlite:DatabaseFile

My question is, how to include the SQLite database in the executable Jar?

EDIT

When I placed the DB file in the source Folder Project/src/DatabaseFile and changed the path to jdbc:sqlite:src/DatabaseFile, it worked on Eclipse but again when running the Jar file as java -jar Project.jar. It said:

path to 'src/DatabaseFile': 'C:\Users\name\src' does not exist

I think I need to specify a relative path for the database.

EDIT

This is how I connect to the database:

public Connection getConnection(){      
    try{
        Class.forName("org.sqlite.JDBC").newInstance();             
        con = DriverManager.getConnection("jdbc:sqlite:src/DatabaseFile");              

    } catch (Exception e) {
        Log.fatal("Méthode: getConnection() | Class  : SQLiteConnection | msg system : " + e.getMessage());
    }
    return con;
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Momo
  • 2,471
  • 5
  • 31
  • 52
  • Had you added this **jar file** to the Source Folder inside the Project ? I guess that might can help. I hope this [answer](http://stackoverflow.com/a/9866659/1057230) might can help you a bit. – nIcE cOw Aug 18 '12 at 16:01
  • I added the database in the src folder (see update). Is there any way to connect to the database as a file instead of using con = DriverManager.getConnection("jdbc:sqlite:src/DatabaseFile"); ? – Momo Aug 18 '12 at 17:35
  • Source Folder doesn't mean arc Folder, instead right click your project -> New -> Source Folder. I am not near my PC, else might have looked at it myself, to at least provide you with working example. – nIcE cOw Aug 18 '12 at 18:12
  • I had tried to do this with my SQL Server Drivers on Eclipse, I found out how to add the .jar to your project. Shall I describe the steps to you as an answer, since it will be in detail then all steps one by one. You can change the connection code and .jar file with your respective stuff. – nIcE cOw Aug 19 '12 at 08:49

2 Answers2

46

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.

dsh
  • 12,037
  • 3
  • 33
  • 51
  • 3
    I was using sqlitejdbc-v056. So I changed it to sqlite-jdbc-3.6.14.1 and I connected to the database with this path "jdbc:sqlite::resource:DatabaseFile" (the database file is under src folder). It worked fine in Eclipse AND in the exporter Jar. Thanks – Momo Aug 19 '12 at 16:48
  • this answer worth a lot more than one +1. Thanks – Dan Apr 25 '15 at 22:25
3

I have found two different ways to name the filepath depending on how you are trying to access it. Assuming you are accessing the db is located in /yourproject/resource/ or /yourproject/bin/resource ( havent narrowed it down, mine is in both and I'm happy with it) you should use this as your path:

//Eclipse test path
String url = "jdbc:sqlite:resource/mydb.db";

or

//runnable jar path
String url = "jdbc:sqlite::resource:mydb.db";

then

mysqlitedatasource.setUrl(url);

Your way also works... by putting the db in /src

tricknology
  • 1,092
  • 1
  • 15
  • 27