简体   繁体   English

ClassNotFoundException与DriverManger.getConnection一起抛出?

[英]ClassNotFoundException gets thrown with DriverManger.getConnection?

Would like to know whether or not potentially the ClassNotFoundException could be get thrown in the following scenario: 想知道在以下情况下是否可能引发ClassNotFoundException

DriverManager.getConnection(...);

I know it could be get thrown when loading manually the drivers Class.forName(drivers..) on the other hand from JDBC 4.0 onwards there is no need of doing it manually as class DriverManager automatically does the job. 我知道,当从JDBC 4.0开始手动加载驱动程序Class.forName(drivers..)时,可能会抛出该错误,因为类DriverManager会自动执行此工作,因此无需手动执行此操作。 My question is: when automatically loading the drivers, if not found the right driver, does it throws the ClassNotFoundException ? 我的问题是:自动加载驱动程序时,如果找不到正确的驱动程序,会引发ClassNotFoundException吗? I see in the API of DriverManager it throws only SQLException and it's not related in anyhow to ClassNotFoundException. 我在DriverManager的API中看到它仅引发SQLException,并且与ClassNotFoundException无关。

Does it mean it won't throw it? 这是否意味着它不会抛出? Thanks, 谢谢,

Indeed ItIs 的确是

Its the Class.forName method that throws ClassNotFoundExceptionhttp not DriverManager.getConnection(...). 它的Class.forName方法抛出ClassNotFoundExceptionhttp而不是DriverManager.getConnection(...)。 See forName documentation. 请参阅forName文档。

Also make sure that that you provided the fully qualified class name as class.forName() expects a fully qualified class name 还要确保您提供的全限定类名称为class.forName()需要全限定的类名称

No, it could not throw ClassNotFoundException . 不,它不能抛出ClassNotFoundException With JDBC4, if the Driver isn't registered, you'll just get an exception stating that no registered driver exists for that url. 使用JDBC4,如果未注册Driver ,则只会得到一个异常,指出该URL不存在已注册的驱动程序。 Something like 就像是

Exception in thread "main" java.sql.SQLException: No suitable driver found for <your url>
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at ru.expbrain.flib.Main.main(Main.java:15)

The javadoc for DriverManager.getConnection(String) states DriverManager.getConnection(String)状态的Javadoc

The DriverManager attempts to select an appropriate driver from the set of registered JDBC drivers. DriverManager尝试从已注册的JDBC驱动程序集中选择适当的驱动程序。

It throws SQLException, something like java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test . 它抛出SQLException,类似于java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test It is stated in DriverManager.getConnection API 在DriverManager.getConnection API中声明

Throws: SQLException - if a database access error occurs

You know you can look at the source of DriverManager to see how it loads Driver implementations from the classpath (the source is included in the JDK). 您知道您可以查看DriverManager的源代码,以了解它如何从类路径中加载Driver实现(源代码包含在JDK中)。

Or you can look at: 或者您可以看一下:

The loadInitialDrivers will ignore any exceptions thrown when iterating over the classes returned by the ServiceLoader iterator. 在对ServiceLoader迭代器返回的类进行迭代时, loadInitialDrivers将忽略引发的任何异常。

Also as ClassNotFoundException is a checked exception and getConnection() does not declare to throw it, you should have known it will never be able to throw that exception (unless wrapped in a different exception). 同样,由于ClassNotFoundException是一个检查的异常,并且getConnection()没有声明抛出该异常,因此您应该知道它永远无法抛出该异常(除非包装在其他异常中)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM