JDBC + SQLite:DriveManager不会加载所需的驱动程序 [英] JDBC + SQLite: DriveManager does not load needed driver

查看:120
本文介绍了JDBC + SQLite:DriveManager不会加载所需的驱动程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用jdbc,SQLite和Java的DriveManager时遇到一些奇怪的错误.根据相应的 java文档只要提供了jdbc,就可以自己加载所需的驱动程序.但是,对我来说,这在使用SQLite时不起作用:

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");

执行此代码时,我得到一个SQLException:No suitable driver found for jdbc:sqlite:pathToFile\database.db通常,我会假设jdbc不是应该的,但是出于某些原因,以下工作:

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");

哪个让我完全不知道为什么第一个版本不起作用.附带说明一下,遵循相同实现的MySQL也可以很好地工作,而无需使用Class.forName().一些帮助,将不胜感激.

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.

推荐答案

这取决于jdbc驱动程序的实现方式.在过去,在加载驱动程序之前,您总是需要Class.forNames()调用.使用jdbc ServiceLoader支持实现的现代"驱动程序不再需要此功能.基本上,这取决于驱动程序jar是否具有适当的"META-INF/services/java.sql.Driver"条目.

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.

DriverManager

All of this is covered in the DriverManager javadocs.

这篇关于JDBC + SQLite:DriveManager不会加载所需的驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆