问题
I am trying to connect to a sqlite database from Java. I am programming in Windows. I downloaded the sqlite driver and set the classpath in the windows environment variables. I get a classNotFoundException when this code is called:
Class.forName(sDriver);
where sDriver is "org.sqlite.JDBC" Here is the stacktrace from the exception:
java.lang.ClassNotFoundException: org.sqlite.JDBC
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.ib.client.examples.Mydb.setConnection(Mydb.java:49)
at com.ib.client.examples.Mydb.init(Mydb.java:33)
at com.ib.client.examples.Mydb.<init>(Mydb.java:17)
at com.ib.client.examples.Example1.run(Example1.java:40)
Any help would be greatly appreciated. Thanks.
回答1:
I don't know which JDBC driver you are trying to use, but the best SQLite JDBC driver i found so far is the one made by the Xerial Project: http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC
You can find the latest version of the driver here: http://www.xerial.org/maven/repository/snapshot/org/xerial/sqlite-jdbc/3.7.8-SNAPSHOT/
It uses a native driver for Windows (32 and 64 bits), Linux (32 and 64 bits) and MacOS (32 and 64 bits), and falls back to a pure java implementation in the case of an unsupported platform.
All native libraries (dll, so, etc) are included in the driver's JAR file, and you don't need to worry about loading any of them. It will autodetect the right library for you.
EDIT: If you are trying to run your program from command line, you must specify every jar file in the classpath. Setting their path in the classpath environment variable is not enough.
Example:
java YourProgram -cp classes;lib/sqlite.jar;c:\libs\java\lib1.jar
来源:https://stackoverflow.com/questions/11361500/issues-registering-sqlite-driver-in-java