Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a database?

Tags:

java

jdbc

What will the command

Class.forName("oracle.jdbc.driver.OracleDriver") 

exactly do while connecting to a Oracle database? Is there an alternate way of doing the same thing?

like image 361
Aravind Avatar asked Nov 08 '11 15:11

Aravind


People also ask

What is the purpose of using class forName Oracle JDBC Oracledriver?

Class and the forName() is a static method of the java. lang. Class . The JDBC Drivers (String) will be loaded into the class dynamically at run time and forName method contains static block which creates the Driver class object and register with the DriverManager Service automatically.

What does JDBC forName () method does for you when you connect to any DB?

The forName() method of Class class is used to register the driver class. This method is used to dynamically load the driver class.

Why do we use class forName com mysql JDBC driver?

It will create a new instance of the com. mysql. jdbc. Driver class and hence call the static initialization which will register the driver with the DriverManager so you can create mysql connections based on the URL you use in the second line.

What is the use of JDBC driver class?

Register the JDBC drivers DriverManager class. This class provides a basic service for managing a set of JDBC drivers. The registerDriver() method takes as input a "driver" class, that is, a class that implements the java. sql.


1 Answers

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") calls show up in legacy code that uses JDBC because that is the legacy way of loading a JDBC driver.

From The Java Tutorial:

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.)

Further reading (read: questions this is a dup of)

  • 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
like image 166
Matt Ball Avatar answered Sep 28 '22 12:09

Matt Ball