0

I'm trying to set up a simple Java application to connect to a SQLite database but keep receiving:

ClassNotFoundException: org.sqlite.jdbc.

I've downloaded the sqlite jdbc driver jar and placed it in the same directory as the .java, and I'm compiling on the command line with:

javac -cp sqlite-jdbc-3.7.2.jar sqlite3test.java

At runtime I then get the above exception. Below is the code:

import java.sql.*;

public class sqlite3test
{
    public static void main(String args[])
    {
        Connection c = null;
        try
        {
            Class.forName("org.sqlite.JDBC");
            c = DriverManager.getConnection("jdbc:sqlite:cs261.db");
        }
        catch ( Exception e )
        {
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Opened database successfully");
    }
}

How do I fix this issue? Thanks.

7
  • How do you execute your application? You show how you compile, not how you execute. My guess is you didn't specify the sqlite dependency on the classpath when running the application. Note that when you compile, you probably don't need the sqlite dependency (as long as you programmed against the JDBC interfaces). Commented Feb 26, 2017 at 10:32
  • Are you suggesting running like 'java -cp sqlite-jdbc-3.7.2.jar sqlite3test'? Because that just gives me an error saying 'Could not find or load main class' Commented Feb 26, 2017 at 11:05
  • No, you need java -cp sqlite-jdbc-3.7.2.jar;. sqlite3test (replace the ; with : if you are not on Windows) Commented Feb 26, 2017 at 11:59
  • I get a SQLException: No suitable driver found for jdbc:sqlite:cs261.db from that still. Commented Feb 26, 2017 at 12:03
  • 1
    I downloaded sqlite-jdbc-3.16.1.jar, copied your code, put both in the same folder, compiled it with javac *.java and then ran it with java -cp sqlite-jdbc-3.16.1.jar;. sqlite3test and it works. Commented Feb 26, 2017 at 12:22

1 Answer 1

1

Check the database version installed, in my case it was: SQLite version 3.8.2

I am using sqlite-jdbc-3.8.9.1.jar which is the greater version number than of the database installed and it is working perfectly.

You have to make sure the jar file version should be greater than or equal to the database version installed in your system.

From the latest version of java (from Java 6+) you don't need to load your class file.

Remove the following line and try it should work.

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

Note: I suggest you to rename your class name from sqlite3test to Sqlite3Test though its not mandatory. According to the JAVA code convention class name should always start with Caps letter.

There are different ways to load the driver into your classpath:

  • place the jar into the $JAVA_HOME/jre/lib/ext folder
  • using maven dependencies
  • using java -cp
  • if you are using the IDE like eclipse then right click the project -> build path -> configure build path -> libraries -> add external jars and locate your jar there.

For your reference : http://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/

Sign up to request clarification or add additional context in comments.

16 Comments

If I remove that line I get this error: java.sql.SQLException: No suitable driver found for jdbc:sqlite:cs261.db
try placing the sqlite-jdbc-3.7.2.jar file inside the $JAVA_HOME/jre/lib/ext folder and see
I still get the same SQLException :(
Which java version you are using? java -version on your terminal
Version 1.7.1_121
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.