Summary: in this tutorial, you will learn how to download SQLite JDBC Driver and connect to the SQLite database via JDBC.
Download SQLite JDBC Driver
To download the latest version of SQLite JDBC Driver, you go to the download page. You should download the latest version of the driver. At the time of this writing, the latest version is sqlite-jdbc-3.27.2.1.jar
.
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:
jdbc:sqlite:sqlite_database_file_path
Code language: Java (java)
The sqlite_data_file_path
is the path to the SQLite database file, which is either a relative or absolute path as follows:
jdbc:sqlite:sample.db
Code language: SQL (Structured Query Language) (sql)
Or
jdbc:sqlite:C:/sqlite/db/chinook.db
Code language: Java (java)
To connect to an in-memory database, you use the following connection string:
jdbc:sqlite::memory:
Code language: Java (java)
Connect to an SQLite database via JDBC
Step 1
Create a new directory called java under c:\sqlite
Step 2
Inside the java
directory create a new directory called connect
.
Step 3
Copy the jar file sqlite-jdbc-3.27.2.1.jar
to the c:\sqlite\connect
folder.
Step 4
Create a new subdirectory called net
inside c:\sqlite\connect\
and another subdirectory called sqlitetutorial
inside the net
directory.
Step 5
Create a new file named Connect.java
in the sqlitetutorial
directory with the following contents. The program will connect to the chinook.db
database located in the c:\sqlite\db\
folder.
package net.sqlitetutorial;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
/**
*
* @author sqlitetutorial.net
*/
public class Connect {
/**
* Connect to a sample database
*/
public static void connect() {
Connection conn = null;
try {
// db parameters
String url = "jdbc:sqlite:C:/sqlite/db/chinook.db";
// create a connection to the database
conn = DriverManager.getConnection(url);
System.out.println("Connection to SQLite has been established.");
} catch (SQLException e) {
System.out.println(e.getMessage());
} finally {
try {
if (conn != null) {
conn.close();
}
} catch (SQLException ex) {
System.out.println(ex.getMessage());
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
connect();
}
}
Code language: Java (java)
Note that you should have the chinook.db
file downloaded and copied to the C:/sqlite/db/
directory.
Step 6
Launch the command line window and navigate to the sqlitetutorial
subfolder created above using the following command:
cd c:\sqlite\java\connect\net\sqlitetutorial
Code language: Shell Session (shell)
Step 7
Compile the Connect.java
file using the following command:
javac Connect.java
Code language: SQL (Structured Query Language) (sql)
You will see a new class file generated:
Note that your JDK must be on the PATH, otherwise you’ll encounter an error.
Step 8
Change the current directory to the connect
directory:
c:\sqlite\java\connect\net\sqlitetutorial>cd..
c:\sqlite\java\connect\net>cd..
Code language: Shell Session (shell)
Step 9
Run the net.sqlitetutorial.Connect
class using the following command:
java -classpath ".;sqlite-jdbc-3.27.2.1.jar" net.sqlitetutorial.Connect
Code language: Shell Session (shell)
Here is the output:
Connection to SQLite has been established.
Code language: Shell Session (shell)
The output shows that the program creates a connection to the SQLite database file successfully.
Troubleshooting
If you receive the following message, you should have missed step 8:
Error: Could not find or load main class net.sqlitetutorial.Connect
Code language: Shell Session (shell)
How the program works
In the connect()
method:
First, declare a variable that holds a connecting string to the sqlite database c:\sqlite\db\chinook.db
String url = "jdbc:sqlite:C:/sqlite/db/chinook.db";
Code language: Java (java)
Next, use the DriverManager
class to get a database connection based on the connection string.
conn = DriverManager.getConnection(url);
Code language: Java (java)
Then, trap any SQLException
in the trycatch
block and display the error message.
After that, close the database connection in the finally
block.
Finally, call the connect()
method in the main()
method of the Connect
class.
In this tutorial, you have learned step by step how to use the SQLite JDBC driver to connect to an SQLite database from a Java program.