Access SQLite within tha JAR File


By Shoaib in: Java // August 25, 2011

Embedding the database within the Application is really a good idea ,to get the things work out as planned you need a correct choice,using SQLite you can embed the database within the jar file along with the Application,unlike Derby it doesn’t creates a dabase outside the Application JAR but instead you can have it within the Application JAR,Firstly you need a JDBC driver for making a connection to the SQLite database with JAVA, Download the ‘sqlite-jdbc-3.7.2.jar’,which is the latest one as of for now,but check to see if there is another release from SQLite,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.
TO Configure the DataBase ,create an Object of SQLiteConfig class and set read Only property to false and now you can connect to and write or read from the database,the important thing to note here is that you need the sqlite-jdbc JAR not only during compiling but also at the run time,so don’t forget to extrat the JAR file and add into the application JAR

*author : shoaib
import org.sql.*;
import org.sqlite.*;
public class ConnectingSQLite
private Connection conn;
public static void main(String[] args)

		try {

			SQLiteConfig config =
                      new SQLiteConfig();
			SQLiteDataSource ds=
                       new  SQLiteDataSource(config);
	    	conn = ds.getConnection();
		 }catch(SQLException se)
	//This method return more than one error from SQL,
System.out.println("SQLError: "+se.getMessage()+" code: "
       +se.getErrorCode()+" SQL State: "+se.getSQLState());


3 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Thank you! I have googled such problem until found your soluiton. You’re awesome!

    Comment by Lawrence Rao — December 23, 2012 @ 3:08 am

  2. First, thank you for all.
    I put database into jar as you mention. Only I insert some values using my runable jar file application. I dont drop and create database everytime when application opening. Everything ok.
    But when i put that jar file to another pc, my datas return to first state, i found my first records. I think sqlite save record another place in pc.
    In the same way i opened database in the jar file using SQLiteDatabaseBrowserPortable. I found again my first records.

    Is there a way to overcome it.

    Sorry about my bad English.

    Comment by Fahrettin ERDOGDU — February 22, 2013 @ 3:38 pm

  3. I found the answer to my question.
    DB files will be extracted to a temporary folder specified in System.getProperty(“”).
    I think it will be read and write in temporary database. I can find my all records in temporary database.

    Comment by Fahrettin ERDOGDU — February 22, 2013 @ 4:39 pm

Leave a Comment

Some HTML is OK

Or reply via TrackBack URL

Follow Me Over Here On twitter