aspose file tools*
The moose likes JDBC and the fly likes Class.forName(String) usage in JDBC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Class.forName(String) usage in JDBC" Watch "Class.forName(String) usage in JDBC" New topic
Author

Class.forName(String) usage in JDBC

Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
In JDBC, we generally load the driver class using the below statement:

Class.forName(driverclassname);

I have queries:

i. Why do we need to load the driver class using this statement?

ii. Can't we include the driver class on our classpath and hence, can be loaded during the application startup itself along with other classes?

iii. What is the benefit we do achieve using this statement in our code?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39811
    
  28
I think I shall move this discussion to our databases fora.
You have to include the driver in the classpath. Otherwise the Class.forName() invocation will not find it.
The driver classes are designed to be used like that. They have static initialisers which you cannot see which load the actual database management program. These static initialisers are executed when the class is loaded, so all you need to write is Class.forName().
harshvardhan ojha
Ranch Hand

Joined: Jul 26, 2007
Posts: 157
    
    1

Class.forname loads class dynamically at runtime.
It has static initializer which looks like
static {
try {
java.sql.DriverManager.registerDriver(new Driver());
} catch (SQLException E) {
throw new RuntimeException("Can't register driver!");
}
}
So after loading driver, it initializes that class properly(forname method is static).
Vaibhav G Garg
Ranch Hand

Joined: Sep 23, 2011
Posts: 140
Thanks Cambell for your prompt response!

i am sorry but, if the class is loaded by the class loader as other classes, even then the static intializers will be run. So, I didn't get the actual benefit.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39811
    
  28
You ’re welcome
As I said, the static initialiser is run when the class is loaded. I don’t understand why you are not getting the benefit.
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2510
    
  10

This isn't needed anymore when using JDBC 4.0 drivers. All the 4.0 ones in your classpath are automagically loaded.


OCUP UML fundamental and ITIL foundation
youtube channel
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2510
    
  10

Vaibhav G Garg wrote:i. Why do we need to load the driver class using this statement?

Part of the pre-jdbc 4.0 api. The class has a static piece of code to register itself. When a class is loaded in Java, static methods are executed by the VM as part of the load activity.

You load the class -> The class runs its static methods -> Driver registered

Vaibhav G Garg wrote:ii. Can't we include the driver class on our classpath and hence, can be loaded during the application startup itself along with other classes?

From jdbc 4.0 on: yes. Pre-jdbc 4.0: no, because putting these drivers in the classpath didn't run their registration static code.


Vaibhav G Garg wrote:iii. What is the benefit we do achieve using this statement in our code?

Pre-jdbc 4.0 only:
The driver registers itself.
and when you open a jdbc connection in your program, the jdbc api will iterate over all its registered drivers, and will give the task to connect to the first one that recognizes the connection string.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

It doesn't run any static methods because the class is loaded. It runs static initializer blocks, as shown in harshvardhan ojha's post.
The JDBC 4.0 mechanism uses ServiceLoader to load the Driver classes and initialize them. The current implementation it doesn't do anything with the instances, so it still requires the driver classes to register themselves in a static initializer block. Therefore, the only difference is the absence of the Class.forName calls. The rest of the mechanism stays exactly the same. And you can still use Class.forName as it will not initialize the class again if it already has been done.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2510
    
  10

Rob Spoor wrote:It doesn't run any static methods because the class is loaded. It runs static initializer blocks, as shown in harshvardhan ojha's post....

Yes true. I must have been sleeping when I typed that. :à
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Class.forName(String) usage in JDBC