• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on JDBC class.forname..

 
lokesh pattajoshi
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
lokesh pattajoshi
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jong,

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

Thanks,
Lokesh
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this thread ought to be in the JDBC forum. Moving.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15219
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 130
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Jong,

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

    Regards,
    Lokesh
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic