Understanding JDBC with SQLite

JDBC (Java Database Connectivity) is a Java API used to connect a Java application with an SQLite Database and execute SQL statements. In the non-Maven approach, we manually download the SQLite JDBC driver JAR and include it in the classpath while compiling and running the Java program. For Java applications, Xerial SQLite JDBC Driver is commonly used. It provides JDBC connectivity to SQLite databases.

The SQLite database is stored as a file, for example:

jdbc_demo.db

Step 1: Install SQLite Database

SQLite does not require a separate database server to be running. The database is created as a file when the Java application connects to it.

For this example, we will use:

Database File : jdbc_demo.db

The JDBC URL will be:

jdbc:sqlite:jdbc_demo.db

The database file can be located inside the project directory.

Step 2: Download the SQLite JDBC Driver

Download the SQLite JDBC driver from the official Xerial SQLite JDBC project:

Xerial SQLite JDBC Driver – GitHub

Download the JDBC driver JAR.

The downloaded file will have a name similar to:

sqlite-jdbc-3.x.x.x.jar

The exact version may differ depending on the release.

For this example, assume:

sqlite-jdbc-3.x.x.x.jar

Step 3: Create the Project Folder Structure

Create the following folders:

SQLiteJDBC/

├── sqlite-jdbc-3.x.x.x.jar

├── StudentApp.java
sqlite-ai

Step 4: Create the Database Table

SQLite does not require you to create a database server or manually create a database first.

The database file:

jdbc_demo.db

will be created automatically when the Java program establishes the connection.

We can create the student table from Java.

The SQL statement is:

CREATE TABLE student (
    id INTEGER PRIMARY KEY,
    name VARCHAR(50)
);

We can also insert sample data:

INSERT INTO student VALUES (1, ‘Raja’);
INSERT INTO student VALUES (2, ‘Ram’);

Step 5: Write the JDBC Program

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class StudentApp {

    public static void main(String[] args) {

        try {

            // Load SQLite JDBC Driver
            Class.forName("org.sqlite.JDBC");

            // Create Connection
            Connection con = DriverManager.getConnection(
                "jdbc:sqlite:jdbc_demo.db"
            );

            // Create Statement
            Statement st = con.createStatement();

            // Create student table
            st.executeUpdate(
                "CREATE TABLE IF NOT EXISTS student (" +
                "id INTEGER PRIMARY KEY, " +
                "name VARCHAR(50))"
            );

            // Insert sample data
            st.executeUpdate(
                "INSERT OR IGNORE INTO student VALUES (1, 'Raja')"
            );

            st.executeUpdate(
                "INSERT OR IGNORE INTO student VALUES (2, 'Ram')"
            );

            // Execute Query
            ResultSet rs = st.executeQuery(
                "SELECT * FROM student"
            );

            // Display Results
            while (rs.next()) {

                System.out.println(
                    rs.getInt("id") + " " +
                    rs.getString("name")
                );
            }

            // Close Resources
            rs.close();
            st.close();
            con.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Step 6: Open Command Prompt or PowerShell

Navigate to the project folder.

For example:

cd D:\SQLiteJDBC

Step 7: Compile Using the Classpath

Compile the Java program:

javac -classpath “.;D:\SQLiteJDBC\sqlite-jdbc.jar” StudentApp.java

Run the program using:

java -classpath “.;D:\SQLiteJDBC\sqlite-jdbc.jar” StudentApp

Execution and Output:

PS D:\SQLiteJDBC> javac -classpath ".;D:\SQLiteJDBC\sqlite-jdbc.jar" StudentApp.java
PS D:\SQLiteJDBC> java -classpath ".;D:\SQLiteJDBC\sqlite-jdbc.jar" StudentApp
1 Raja
2 RamCode language: CSS (css)
Scroll to Top