File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes Doubt on JDBC class.forname.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Doubt on JDBC class.forname.." Watch "Doubt on JDBC class.forname.." New topic
Author

Doubt on JDBC class.forname..

lokesh pattajoshi
Ranch Hand

Joined: Jul 29, 2009
Posts: 130
Hi All,

I have a doubt in JDBC..I know that Class.forName is used to load a driver . What is the meaning of Loading a driver ? What exactly this line "" is performing ? Why we are calling a driver through Class.forName instead of following normal process of Class initialization..?

Please help me to understand.

Regards,
Lokesh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14428
    
  23

forName is a method in class java.lang.Class. You can look it up in the API documentation.

What it does is find a class by name and load the class. Part of loading the class is running the static initializers in the class. Normally, JDBC driver classes internally have a static initializer that registers the driver in an internal JDBC driver registry, so that JDBC knows the driver class.

Note that in the latest version of the JDBC API (I think JDBC 4.0 if I remember correctly, which is included with Java SE 6) the mechanism to find and load drivers has been changed, so that it isn't really necessary anymore to do Class.forName("...") to load the driver. You only need to do this if you're using an older driver or an older version of Java.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
lokesh pattajoshi
Ranch Hand

Joined: Jul 29, 2009
Posts: 130
Hi Jong,

Thanks for your valuable information..What is registering the Driver? how it works?

Thanks,
Lokesh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Maybe this thread ought to be in the JDBC forum. Moving.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14428
    
  23

The driver registers itself, in a registry of JDBC drivers somewhere under the covers. As a user of the JDBC API you don't need to know the details of how this works.

When you open a JDBC connection, like this:

then DriverManager will look for a JDBC driver that accepts the given JDBC URL. In this case, it's a JDBC URL for the Oracle JDBC driver.

But the DriverManager has to know where the JDBC drivers are to be able to find one that can handle the URL. That's what that registry is for. So:

  • You call Class.forName("oracle.jdbc.driver.OracleDriver");
  • This will run the static initializer in the driver class, which registers the driver in DriverManager's registry
  • You call the line of code above to get a connection
  • The DriverManager will ask the drivers in its registry if they can handle the URL
  • It will find the Oracle driver and create a connection using that driver.

  • lokesh pattajoshi
    Ranch Hand

    Joined: Jul 29, 2009
    Posts: 130
    Hi Jong,

    Thank you very much for your clear information which clarified all my doubts.

    Regards,
    Lokesh
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Doubt on JDBC class.forname..