Monday, April 30, 2012

Why we use Class.forName with the jdbc driver?

The response for this is too simple.

The JDBC driver needs to be registered in the DriverManager class, for make the registration of the driver the runtime needs to call the implementation driver class this class can be the SQLServerDriver or the OracleDriver class, when the java runtime load this class a static block of code is executed and this execution of code make the registration of the driver in the DriverManager class.

Why we need to register a driver well if you do not register the driver, you are not able to get a connection from the DriverManager because the driver manager does not know anything about the database driver and does not have a register of the database driver. 

In the java doc for the DriverManager class you can see the following methods that help to register a database driver:



registerDriver(Driver driver) 

Javadoc for Driver Manager: http://docs.oracle.com/javase/7/docs/api/

Note: for new drivers this behaviors is not more needed because the registration of the class is executed by a service registration in the META-INF files if you want more information about that, you can find in internet "Service Provider implementations in Java".

No comments:

Post a Comment

Revolutionizing Stream Pipelines with Custom Intermediate Operations JEP 473: Stream Gatherers (Second Preview)

Introduction In the ever-evolving scene of Java improvement, the Stream API has been a foundation of utilitarian programming paradigms sin...