I experience some strange errors with jdbc, SQLite and java’s DriveManager. According to the corresponding java docs, DriveManager is able to load needed drivers by himself, as long as jdbc is provided. However, for me this does not work when using SQLite:
Connection conn = DriverManager.getConnection("jdbc:sqlite://" + pathToFile + File.separator + "database.db");
When executing this code, I get an SQLException: No suitable driver found for jdbc:sqlite:pathToFile\database.db Normally I would assume that jdbc is not were it should be, but, for some reason, the following works:
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite://" + pathToFile + File.separator + "database.db");
Which leaves me completely clueless on why the first version does not work. As a side note, MySQL following the same implementation works fine without using Class.forName() too. Some help would be appreciated.
It depends on how the jdbc driver is implemented. In the “old” days, you always needed the
Class.forNames()call before loading the driver. “modern” drivers implemented using the jdbc ServiceLoader support no longer require this. basically it comes down to whether or not the driver jar has the appropriate “META-INF/services/java.sql.Driver” entry.All of this is covered in the DriverManager javadocs.