gpt4 book ai didi

java - 了解 JDBC 内部结构

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:04:53 25 4
gpt4 key购买 nike

[1] 在JDBC中,为什么要先用Class.forName("some driver name")加载驱动。为什么 SUN 没有在 getConnection() 方法本身中处理加载驱动程序。如果我将驱动程序名称作为参数传递给 getConnection() 方法。

[2] 我想了解 JBDC 的内部结构。感谢任何指向它的指示。

最佳答案

使用 JDBC 4,您不再需要使用 Class.forName(...) 参见 here一篇解释这一点的文章:

Connection to a database requires that a suitable JDBC database driver be loaded in the client's VM. In the early days of JDBC, it was common to load a suitable driver via Class.forName(), passing in the name of the class implementing the JDBC Driver interface. The DriverManager class later offered a more flexible means of managing JDBC drivers in a client application. For a driver to become available, DriverManager's registerDriver() had to be invoked with the driver's class name. Alternatively, you could specify the drivers to load via the jdbc.drivers system property. When DriverManager initializes, it attempts to load the drivers associated with that property.

JDBC 4 adds the J2SE Service Provider mechanism as another means to specify database drivers. For this to work, driver JAR files must include the file META-INF/services/java.sql.driver. That file must contain a single line with the name of the JDBC driver's implementation of the Driver interface. Invoking getConnection() on DriverManager will load a driver so packaged, if needed. Upon loading the driver, an instance of the driver is created, and then registerDriver() is invoked to make that driver available to clients.

看看Sun's JDBC link有关 JDBC 的更多信息。与其他一些规范相比,JDBC 4.0 规范是一本相对不错的读物...

关于java - 了解 JDBC 内部结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/269160/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com