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; }
Copy the jar file sqlite-jdbc-3.27. 2.1. jar to the c:\sqlite\connect folder.
To establish a database connection to an SQLite database, you need to create a new instance of the PDO class and pass a connection string to the constructor of the PDO object. Because you store the path to the sqlite database file in the Config class, you just simply use it to construct the connection string.
The JAR file includes both Java class files and SQLite binaries for Mac OX S, Linux, and Windows, Both 32-bit and 64-bit. SQLite connection strings. The SQLite JDBC driver allows you to load an SQLite database from the file system using the following connection string.
The SQLite JDBC driver allows you to load an SQLite database from the file system using the following connection string: The sqlite_data_file_path is the path to the SQLite database file, which is either relative or absolute path as follows:
If you are going to use Windows machine, then you can compile and run your code as follows − $javac SQLiteJDBC.java $java -classpath ".;sqlite-jdbc-3.7.2.jar" SQLiteJDBC Opened database successfully Following Java program will be used to create a table in the previously created database.
JDBC drivers are Java library files with the extension .jar used by all Java applications to connect to the database. Usually, they are provided by the same company which implemented the Sqlite software. DbSchema Tool already includes an Sqlite driver, which is automatically downloaded when you connect to Sqlite.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With