This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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

 
Does this tiny ad smell okay to you?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic