连接数据库时Class.forName(&oracle.jdbc.driver.OracleDriver")的实际用途是什么? [英] What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a database?
问题描述
命令是什么
Class.forName("oracle.jdbc.driver.OracleDriver")
连接到Oracle数据库时究竟做了什么?是否有另一种方法可以做同样的事情?
exactly do while connecting to a Oracle database? Is there an alternate way of doing the same thing?
推荐答案
它使用FQCN获得对类对象的引用(完全限定)类名) oracle.jdbc.driver.OracleDriver
。
It obtains a reference to the class object with the FQCN (fully qualified class name) oracle.jdbc.driver.OracleDriver
.
它不会做任何事情连接到数据库除了确保当前类加载器加载指定的类。写作之间没有根本区别
It doesn't "do" anything in terms of connecting to a database, aside from ensure that the specified class is loaded by the current classloader. There is no fundamental difference between writing
Class<?> driverClass = Class.forName("oracle.jdbc.driver.OracleDriver");
// and
Class<?> stringClass = Class.forName("java.lang.String");
Class.forName(com.example.some.jdbc.driver )
调用显示在使用JDBC的 legacy 代码中,因为这是 legacy 加载JDBC驱动程序的方式。
Class.forName("com.example.some.jdbc.driver")
calls show up in legacy code that uses JDBC because that is the legacy way of loading a JDBC driver.
来自 Java教程:
在以前的JDBC版本中,要获得连接,首先必须通过调用方法
Class.forName
来初始化JDBC驱动程序。此方法需要类型为java.sql.Driver
的对象。每个JDBC驱动程序都包含一个或多个实现接口的类java.sql.Driver
。
...
任何JDBC将自动加载在类路径中找到的4.0驱动程序。 (但是,必须使用Class.forName
方法在JDBC 4.0之前手动加载任何驱动程序。)
In previous versions of JDBC, to obtain a connection, you first had to initialize your JDBC driver by calling the method
Class.forName
. This methods required an object of typejava.sql.Driver
. Each JDBC driver contains one or more classes that implements the interfacejava.sql.Driver
.
...
Any JDBC 4.0 drivers that are found in your class path are automatically loaded. (However, you must manually load any drivers prior to JDBC 4.0 with the methodClass.forName
.)
进一步阅读 (阅读:这是一个重复的问题)
- 什么如果您不使用返回值,则会使用Class.forName()吗?
- Class.forName()如何工作?
- 什么'Class.forName(" org.sqlite.JDBC");'做什么?
- 'Class.forName(" MY_JDBC_DRIVER")'的目的是什么?
- 加载JDBC驱动程序
- What purpose does Class.forName() serve if you don't use the return value?
- How does Class.forName() work?
- What does 'Class.forName("org.sqlite.JDBC");' do?
- What is the purpose of 'Class.forName("MY_JDBC_DRIVER")'?
- Loading JDBC driver
Further reading (read: questions this is a dup of)
这篇关于连接数据库时Class.forName(&oracle.jdbc.driver.OracleDriver")的实际用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!