Exact meaning of Class.forName(); in JDBC

Before going to discuss, need to discuss about Drivers.

Inorder to connect a  java Application  with database ,  we need to follow the below important steps

  1. Use any JDBC  Driver class.
  2. This JDBC Driver class has to be instantiated.
  3. And Instantiated Driver class should be Registered with DriverManager class

For Example:

Let’s take Type1-Driver(sun.jdbc.odbc.JdbcOdbcDriver) which comes with JDK by Default

So Inorder to connect  a java application with  database with this driver we have to write following statements.

sun.jdbc.odbc.JdbcOdbcDriver driver=new sun.jdbc.odbc.JdbcOdbcDriver();


Ok.Then what is Class.forName(“com.Anyclass”) ?

Basically Class.forName("com.Anyclass");   used to load a class.

while loading a class, if that class contains static blocks, and any code which is in that static blocks,  will be executed.

So if write a statement  for Ex:      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); 

code in static block of this driver will be executed while class is loading.

All most all JDBC drivers contains static blocks and these static block contains the code to

1) Instantiate the driver class

2) Register the driver with DriverManager.

Following  is the  static block in JdbcOdbcDriver


JdbcOdbcTracer jdbcOdbcTracer= new JdbcOdbcTracer();


<pre>jdbcOdbcTracer.trace("JdbcOdbcDriver class loaded");</pre>

sun.jdbc.odbc.JdbcOdbcDriver driver=new sun.jdbc.odbc.JdbcOdbcDriver();



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s