• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt on JDBC class.forname..

 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jong,

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

Thanks,
Lokesh
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe this thread ought to be in the JDBC forum. Moving.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Hi Jong,

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

    Regards,
    Lokesh
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic