• 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

Pls clarify my doubts on Class.forName()

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Class.forName() is generally used to load classes dynamically, a general use of this method is to load a driver. My question is:

1. What exactly happens when we call Class.forName("DriverName");. How a driver gets loaded by calling this method.
2. How a DriverManager comes to know that a driver has been loaded when we call DriverManager.getConnection(url,login,pwd);

I need an elaborated view on this. Kindly help me to understand the logic.

Regards,
Ritu
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The forName method loads the driver class into the JVM. As per the javadocs of java.sql.Driver, the class should register itself with the DriverManager in a static init block (which gets executed during loading the class). That's how the DriverManager knows about available drivers. As to which driver to use for a particular DB URL, that's where the Driver.acceptsURL method comes into play.
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ritu Kapoor:
Hi,

Class.forName() is generally used to load classes dynamically, a general use of this method is to load a driver. My question is:

1. What exactly happens when we call Class.forName("DriverName");. How a driver gets loaded by calling this method.
2. How a DriverManager comes to know that a driver has been loaded when we call DriverManager.getConnection(url,login,pwd);

I need an elaborated view on this. Kindly help me to understand the logic.

Regards,
Ritu



Did you by any chance have an interview with Accenture Bangalore?
Ulf has supplied your answer.


The second approach is not used because it tightly couples your code to the Driver class whereas the first approach provides you a level of indirect and enables you to NOT care about the Driver class until runtime.

I hope that gives you some insights...
anyway, this is something you must read-

The Java SQL framework allows for multiple database drivers.

Each driver should supply a class that implements the Driver interface.

The DriverManager will try to load as many drivers as it can find and then for any given connection request, it will ask each driver in turn to try to connect to the target URL.

It is strongly recommended that each Driver class should be small and standalone so that the Driver class can be loaded and queried without bringing in vast quantities of supporting code.

When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager. This means that a user can load and register a driver by calling

Class.forName("foo.bah.Driver")
- from the API Docs for java.sql.Driver

 
There are 10 kinds of people in this world. Those that understand binary get this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic