I am working with Eclipse (running on Windows 8.1) and want to access to a sqlite database. Sadly im not able to work with the driver even though I defined its location on the CLASSPATH environment varible. The following program is from one of the thousand tutorial sites:
package sqliteconnectiontest;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.DatabaseMetaData;
public class test {
public static void main(String[] args) throws Exception {
// register the driver
String sDriverName = "org.sqlite.JDBC";
Class.forName(sDriverName);
// now we set up a set of fairly basic string variables to use in the
// body of the code proper
String sTempDb = "hello.db";
String sJdbc = "jdbc:sqlite";
String sDbUrl = sJdbc + ":" + sTempDb;
// which will produce a legitimate Url for SqlLite JDBC :
// jdbc:sqlite:hello.db
int iTimeout = 30;
String sMakeTable = "CREATE TABLE dummy (id numeric, response text)";
String sMakeInsert = "INSERT INTO dummy VALUES(1,'Hello from the database')";
String sMakeSelect = "SELECT response from dummy";
// create a database connection
System.out.println(sDbUrl);
Connection conn = DriverManager.getConnection(sDbUrl);
try {
Statement stmt = conn.createStatement();
try {
stmt.setQueryTimeout(iTimeout);
stmt.executeUpdate(sMakeTable);
stmt.executeUpdate(sMakeInsert);
ResultSet rs = stmt.executeQuery(sMakeSelect);
try {
while (rs.next()) {
String sResult = rs.getString("response");
System.out.println(sResult);
}
} finally {
try {
rs.close();
} catch (Exception ignore) {
}
}
} finally {
try {
stmt.close();
} catch (Exception ignore) {
}
}
} finally {
try {
conn.close();
} catch (Exception ignore) {
}
}
}
}
i already downloaded the sqlite-jdbc-3.7.2.jar driver from http://www.xerial.org/maven/repository/artifact/org/xerial/sqlite-jdbc/3.7.2/
The driver is located in:
B:\sqlite-jdbc-3.7.2.jar
My .class File:
B:\Progs\Java\sqlconnectiontest\bin\sqliteconnectiontest\test.class
The CLASSPATH environment variable:
CLASSPATH=B:\sqlite-jdbc-3.7.2.jar;
When I'm trying to execute the program this exception gets thrown:
Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sqliteconnectiontest.test.main(test.java:14)
From what i found across the internet this problem should mean that the JDBC-driver could not be found, though i put its direction on the CLASSPATH environment variable.
This problem is thrown by the
Classl¡Loader
when it is unable to find the required class.It can be caused by two problems:
a) The class you're searching for is not inside the JAR file (you can check it uncompressing the jar).
b) You're not using the CLASSPATH you think.
Could you get sure the CLASSPATH the ClassLoader uses is the same you're setting?
In How to get the path of running java program you can find how to get current CLASSPATH of a running JVM instance.