连接数据库时Class.forName(&oracle.jdbc.driver.OracleDriver")的实际用途是什么? [英] What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a database?

查看:462
本文介绍了连接数据库时Class.forName(&oracle.jdbc.driver.OracleDriver")的实际用途是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

命令是什么

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 type java.sql.Driver. Each JDBC driver contains one or more classes that implements the interface java.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 method Class.forName.)



进一步阅读 (阅读:这是一个重复的问题)



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