Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • 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: 43027
76
  • 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

 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic