SQLite is a simple, small, fast, reliable, server-less, zero-configuration and no-installation SQL database library which is running in-process with the client application. Although there is no official JDBC driver library from www.sqlite.org, there is one provided by www.xerial.org – an XML Database Management System project.

 

1. Download SQLite JDBC driver

You can download the latest version of JDBC driver for SQLite here. The download is categorized by versions, so browse a directory for a specific version you want: 3.5.9, 3.6.16, 3.7.2, etc. As of this writing, the latest version is 3.7.2 which corresponds to the jar file sqlite-jdbc-3.7.2.jar.

Beside Java class files, the jar file includes SQLite binaries for Windows, Linux and Mac (for both 32-bit and 64-bit).

Place the sqlite-jdbc-VERSION.jar into your classpath.


2. SQLite JDBC database connection URL

The SQLite JDBC driver can load a SQLite database from file system or creates one in memory.

Here is the syntax of database connection URL for file system database:

jdbc:sqlite:database_file_path

Where database_file_path can be either relative or absolute path. For example:

jdbc:sqlite:product.db

jdbc:sqlite:C:/work/product.db

And here is the syntax of database connection URL for memory database:

jdbc:sqlite::memory:

jdbc:sqlite:


3. Loading SQLite JDBC driver

With this SQLite JDBC library, you have to load the driver as follows: 

Class.forName("org.sqlite.JDBC");

Or: 

DriverManager.registerDriver(new org.sqlite.JDBC());

 

4. Making SQLite JDBC connection

The following example program creates a connection to a SQLite database file product.db which is in the same directory as the program, prints some database metadata information, and closes the connection:

package net.codejava.jdbc;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * This program demonstrates making JDBC connection to a SQLite database.
 * @author www.codejava.net
 *
 */
public class JdbcSQLiteConnection {

	public static void main(String[] args) {
		try {
			Class.forName("org.sqlite.JDBC");
			String dbURL = "jdbc:sqlite:product.db";
			Connection conn = DriverManager.getConnection(dbURL);
			if (conn != null) {
				System.out.println("Connected to the database");
				DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
				System.out.println("Driver name: " + dm.getDriverName());
				System.out.println("Driver version: " + dm.getDriverVersion());
				System.out.println("Product name: " + dm.getDatabaseProductName());
				System.out.println("Product version: " + dm.getDatabaseProductVersion());
				conn.close();
			}
		} catch (ClassNotFoundException ex) {
			ex.printStackTrace();
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
	}
}

That's Java code example to establish connection to a SQLite database.

To see the coding in action, I recommend you to watch the video below:

 

JDBC API References:

 

Related JDBC Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (JdbcSQLiteConnection.java)JdbcSQLiteConnection.java[ ]1 kB

Add comment

   


Comments 

#5hajar2023-11-23 02:59
how connection with jdk0.8 for neatbaneas I connot downlod jdk
Quote
#4ronald cunningham2023-09-05 12:53
what version of netbeans did you use to create this video. thanks
Quote
#3joe2016-11-28 09:08
THIS IS result we were waiting for...
Quote
#2Vince2014-07-09 10:32
how can i access ow encryopted sqlite database? i tried with ";Password=123" but output is "no tables found". sqlcipher doesn't work in java.
Quote
#1Ambarish Hazarnis2014-03-18 13:38
Thank you :)
Quote