File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Class.forName Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Class.forName" Watch "Class.forName" New topic


Shrawan Bhageria
Ranch Hand

Joined: Jun 20, 2005
Posts: 52
Hi all,

Please explain this.

1>how many ways we can load the driver in jdbc?
2>whats the class.forName(....) do xactly?

Thanks and Regards,

Saket Barve
Ranch Hand

Joined: Dec 19, 2002
Posts: 229
The JDBC driver can be loaded in two ways:
1. Specify the driver(s) on the command line when running your java code, using the colon as a driver-separator:

2. Load the driver from within your application:

As you can see, the Class.forName() performs the task of loading the driver at runtime.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1323

Originally posted by Shrawan Bhageria:

whats the class.forName(....) do xactly?

Using Class.forName will create and initialize an instance of the class you pass in as a paramter. So using Class.forName will achieve the same result as creating an instance of the class and initializing it yourself. The benefits of using Class.forName are that the forName method throws a ClassNotFoundException, so in the case that the classes do not exist that you are trying to use, a ClassNotFoundException will be thrown and since you can't call the forName method without handling the exception in some way (catching or throwing the exception), your application will be better suited to handle this scenario whereas creating instances of the class yourself will not have this benefit (your application will have an unhandled exception and exit). Reference

[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
Joice Jose

Joined: Jun 29, 2005
Posts: 14
If Class.forName will create and initialize an instance of the class you pass, then why not the below program print "Hi making instance of the class.......". But if we use ' new abc(); ' it will print.

public class abc
public abc()
System.out.println("Hi making instance of the class.......");

public static void main(String str[])
try {
}catch(ClassNotFoundException e) {
System.out.println("Hi class not found ......." + e);
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 33111

Good question!

There's a difference between initializing and instantiating. When you load a class into memory (which Class.forName() does), it initializes itself by setting up any static variables. When you instantiate a class, you load it into memory, initialize it and the create an instance.

The JavaDoc for ExceptionInInitializer explains the static intializer relationship a bit more.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Class.forName
It's not a secret anymore!