Using DataSource to connect to SQLite with (Xerial) sqlite-jdbc driver Using DataSource to connect to SQLite with (Xerial) sqlite-jdbc driver sqlite sqlite

Using DataSource to connect to SQLite with (Xerial) sqlite-jdbc driver


My best guess is that I shall use org.sqlite.SQLiteDataSource class (it comes in sqlite-jdbc-3.15.1.jar for Xerial sqlite-jdbc driver),

Yes, that seems likely.

but how?

I just tried the following and it worked for me:

package com.example.sqlite.sqlite_test;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import org.sqlite.SQLiteDataSource;public class SqliteTestMain {    public static void main(String[] args) {        SQLiteDataSource ds = new SQLiteDataSource();        ds.setUrl("jdbc:sqlite::memory:");        try (Connection conn = ds.getConnection()) {            System.out.println("Connected.");            String sql =                     "SELECT COUNT(*) AS n FROM \"sqlite_master\"";            try (                    Statement s = conn.createStatement();                    ResultSet rs = s.executeQuery(sql)) {                rs.next();                System.out.printf(                        "The \"sqlite_master\" table contains %d row(s).%n",                         rs.getInt(1));            }        } catch (SQLException e) {            e.printStackTrace(System.err);        }    }}