Driver Registration In JDBC
An interface links two unrelated objects.A driver in JDBC context is an interface which links java code to database for communication.
Before ever the connection is established between the java programming end and the database end,the driver needs to be registered with the JVM by the means of DriverManager class.The DriverManager class has a vector of drivers registered to it.Once the entry has been made in the vector of the DriverManager class the driver is said to be registered.Each driver has the implementation and is capable of registering itself with the DriverManager class.This implementation is written in the static initialization block of the driver class.
We know that the static initialization block for a class is executed every time we load the class in the class loader. So, in order to run the static initialization block in the class loader we need to run the following line of code so that the driver registers itself with the DriverManager class :
Class.forName("DriverClass");
We can load or compile a class in class loader by calling the Class.forName(“class name”) method of the reflection concept in java.
Now let us have a look at how the static initialization block in the driver class looks like :
class Driver{ static{ -------- DriverManager.registerDriver(Driver Class); ---- ---- ---- } }
The function registerDriver() of the DriverManager class takes driver class as one of the parameters and looks for an entry ofr that driver in the vector it has. If an entry is found it means that the driver is already registered and the reference is returned from the vector.UIf entry is not available in the vector,the entry is made and reference is returned.This way we say that the driver is registered and connection can be made with the database as follows :
DriverManager.getConnection("Connection String","username","password");
Here connection String is depenedent on what database we are using.A typical example is :
jdbc:postgresql://host:port/database
Here ,username and password are username and password for the database.
Number of View :4382Related posts:
- Transaction Locking And Isolation In JDBC Transaction isolation levels Using Transactions JDBC Isolation Example JDBC Guide Basics Of JDBC
- Concept Of Session In J2EE Session Tracking In J2EE Session Management Seesion Concept Servlet Basics Servlet Guide Session Tracking
- How Container Does The Cookie Work in J2EE Define Cookie Cookies Explained Session In Servlet What Is Cookie How Apache Tomcat Handles Cookies Cookies Explained How Cookies Work Cookie Guide
- Constructor And Destructor In C++ Basics Of OPPS Concepts Explained
- Basics of tomcat Apache-Tomcat Tutorial Download and Run Tomcat
Tags: basics of jdbc, class.forName(), database connecivity, database connection, driver, driver manager class, java, jdbc, jdbc connection
This entry was posted on Thursday, May 10th, 2012 at 10:46 am and is filed under Java, OPPS, software, Tech-Tips, Tips & Tricks. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.