Reputation: 11
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.
Upvotes: 0
Views: 1990
Reputation: 1269
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:
For your reference : http://www.sqlitetutorial.net/sqlite-java/sqlite-jdbc-driver/
Upvotes: 1