Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JDBC and SQLite - No suitable driver found no matter what I try

Tags:

java

sqlite

jdbc

I try to make a simple Java program and connect to a SQLite database for learning purposes. But no matter what I try I cannot succeed. I always get No suitabe driver found for jdbc:sqlite:database.db. And yet it should be so simple. I have already tried numerous drivers from Maven repository.

I have read up and I understand the theory: The JDBC API deals with the communication to the JDBC manager, while the JDBC driver deals with the communication to the database. I also understand that I have to add a classpath to the .jar file containing the database driver when I run the compiled class file. As explained here for example.

Some tutorials claim there is a need to register the driver with Class.forName() but Oracle docs says:

Applications no longer need to explicitly load JDBC drivers using Class.forName()]

Also, the SQLite tutorials that I have followed never register the driver before running DriverManager.getConnection(url); Example sqlitetutorial.net

In my directory E:\ProjectI have these files:

DataBase.java
DataBase.class
database.db
sqlite-jdbc-3.44.1.0.jar

When I run the compiled class file, I use java -cp ".;sqlite-jdbc-3.44.1.0.jar" DataBase in the terminal window (Powershell)

And here is my code:

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

public class DataBase {
    public static void connect() {
        Connection conn = null;
        try {
            String url = "jdbc:sqlite:database.db";
            conn = DriverManager.getConnection(url);
            
            System.out.println("Connection is established");
            
            conn.close();
            System.out.println("Connection closed.");

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    } // End method connect

    public static void main(String[] args) {
         connect();

    } // End of main method
} // End of class

I would appreciate any help I could get to push me forward.

I suppose it has to do with the .jar file not being found but I have tried every possible way: Putting copies of the .jar file everywhere in different directories, renaming it to make it simpler, adding multiple directories when adding the class path, etc. I have no clue left of what could be missing.

Also, when I try to do the same in VSCode, by adding the .jar file under "referenced libraries", it still doesn't work.

like image 595
Fivi Avatar asked Sep 02 '25 17:09

Fivi


1 Answers

The problem is that the SQLite JDBC driver also requires the slf4j-api-1.7.36.jar, as detailed in the README on its GitHub repository:

  1. Download sqlite-jdbc-3.44.1.0.jar then append this jar file into your classpath.

  2. Download slf4j-api-1.7.36.jar then append this jar file into your classpath.

  3. Open a SQLite database connection from your code. (see the example below)

Example usage

Assuming sqlite-jdbc-3.44.1.0.jar and slf4j-api-1.7.36.jar are placed in the current directory.

> javac Sample.java
> java -classpath ".;sqlite-jdbc-3.44.1.0.jar;slf4j-api-1.7.36.jar" Sample   # in Windows

In other words, you need to also put slf4j-api-1.7.36.jar in that directory and use:

java -cp ".;sqlite-jdbc-3.44.1.0.jar;slf4j-api-1.7.36.jar" DataBase

On my machine this will make it work:

D:\temp> java -cp ".;sqlite-jdbc-3.44.1.0.jar;slf4j-api-1.7.36.jar" DataBase
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Connection is established
Connection closed.

If you had explicitly added a Class.forName("org.sqlite.JDBC") to your application, you would have been able to diagnose this, because without the slf4-api-1.7.36.jar that would result in (I added Class.forName to the main):

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory
        at org.sqlite.JDBC.<clinit>(JDBC.java:26)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:375)
        at DataBase.main(DataBase.java:23)
Caused by: java.lang.ClassNotFoundException: org.slf4j.LoggerFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
        ... 4 more

So, although explicit loading of the driver is no longer needed, it can give additional troubleshooting information.

like image 104
Mark Rotteveel Avatar answered Sep 05 '25 05:09

Mark Rotteveel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!