php Xiaobian Yuzai encountered a problem when using JDBC and SQLite. No matter how hard he tried, he could not find a suitable driver. JDBC is a standard interface for Java to connect to the database, while SQLite is a lightweight embedded relational database engine. However, although JDBC provides drivers for various databases, there is no driver specifically designed for SQLite. This made the editor confused and unable to connect and operate the SQLite database normally. In the next article, we will explore ways to solve this problem.
For learning purposes, I try to make a simple java program and connect to a sqlite database. But no matter how hard I tried, I couldn't succeed. I always get no driver for jdbc:sqlite:database.db
. But it should be so simple. I have tried many drivers from the maven repository.
I have read and understood the theory: jdbc api handles communication with jdbc manager, while jdbc driver handles communication with database. I also learned that when I run the compiled class file, I have to add the classpath to the .jar file that contains the database driver. For example, as explained here.
Some tutorials claim that you need to use class.forname()
to register the driver, but the oracle documentation says:
applications no longer need to explicitly load jdbc drivers using class.forname()]
Additionally, the sqlite tutorial I followed never registered the driver before running the drivermanager.getconnection(url);
example sqlitetutorial.net
In my directory e:\project
I 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
This 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
If I can get any help to push me forward I would be very grateful.
I guess this has to do with not being able to find the .jar file, but I've tried everything possible: putting a copy of the .jar file anywhere in a different directory, renaming it to make it simpler, in Add multiple directory class paths etc when adding. I don't know what might 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.
The problem is that the sqlite jdbc driver also requires slf4j-api-1.7.36.jar
, see its github repository# for details ##:
slf4j-api-1.7.36.jar into that directory and use:
java -cp ".;sqlite-jdbc-3.44.1.0.jar;slf4j-api-1.7.36.jar" databaseOn my machine this would 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 have explicitly added
class.forname("org.sqlite.jdbc") to your application, you will be able to diagnose this problem, as without
slf4-api -1.7.36.jar will result in (I added
class.forname to
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 moreSo while explicitly loading the driver is no longer required, it can provide additional troubleshooting information.
The above is the detailed content of JDBC and SQLite - no matter what I try I can't find the right driver. For more information, please follow other related articles on the PHP Chinese website!