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.
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.
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.
The forName() method of Class class is used to register the driver class. This method is used to dynamically load the 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.
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 theDriverManager
(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 fileMETA-INF/services/java.sql.Driver
. This file contains the name of the JDBC driver’s implementation ofjava.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.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With