Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we use Class.forName(“oracle.jdbc.driver.OracleDriver”) while connecting to a database?

Tags:

java

jdbc

What is the actual use of Class.forName("oracle.jdbc.driver.OracleDriver") while connecting to a database? Why cant we just import the same class, instead why we are loading it.

like image 649
vidyashi Avatar asked Nov 19 '13 17:11

vidyashi


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.

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 the class forName () does established the connection with database?

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

What is the use of JDBC driver class?

The JVM Managed Runtime requires that you identify a JDBC driver class either as a component of a DataSource object or as a parameter in a connection string. A driver class is the main class for a JDBC driver. They are driver-specific.


2 Answers

The basic idea behind using Class.forName() is to load a JDBC driver implementation. A (normal) JDBC driver must contain a static initializer that registers an instance of the driver implementation with java.sql.DriverManager:

JDBC drivers must implement the Driver interface, and the implementation must contain a static initializer that will be called when the driver is loaded. This initializer registers a new instance of itself with the DriverManager

(from JDBC 4.1, section 9.2)

Since JDBC 4.0 however there is a new way to register drivers: the jar of a JDBC driver needs to include a file /META-INF/services/java.sql.Driver which contains the name(s) of the java.sql.Driver implementations in that jar. When you create a connection using the DriverManager, it will use java.util.ServiceLoader to enumerate all /META-INF/services/java.sql.Driver files in the classpath and load all drivers so they get registered.

The DriverManager.getConnection method has been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC driver’s implementation of java.sql.Driver.

(from JDBC 4.1, section 9.2.1)

The reasons drivers are loaded this way, is that it allows you to decouple an application from the driver (and database) it uses. This means that you can write, compile and even distribute an application without any drivers, you only need to use the interfaces provided in the java.sql (and javax.sql) package - which is part of Java - without needing to access the implementation directly.

The user of the application then adds a valid JDBC driver to the classpath (and configuring things like a connection string) so the application can actually to connect to a database. Before JDBC 4.0, the user would have to specify the driver name so that the application could load it using Class.forName, with a JDBC 4.0 compliant driver and Java 6 or higher this discovery is automatic.

When you load a driver literally with Class.forName("oracle.jdbc.driver.OracleDriver") it might feel like overkill, but if you keep in mind that it could also be a string pulled from a config file (or from user input) you might start to understand why it is so powerful.

Of course this driver independence is not 100%, especially not if your application uses vendor specific SQL. But the theory is that your application can be database independent. JDBC also provides some additional mechanisms to address this, eg JDBC escapes to provide a common syntax that the driver translates to the specific syntax, and DatabaseMetaData which allows you to discover features, reserved words etc which allow you to create or generate compatible queries.

like image 160
Mark Rotteveel Avatar answered Sep 22 '22 04:09

Mark Rotteveel


It`s a legacy way to do so. Importing class you will have extra dependency

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

like image 24
hatesms Avatar answered Sep 24 '22 04:09

hatesms