This week's giveaway is in the Programmer Certification (OCPJP) forum.
We're giving away four copies of Java Mock Exams (software) and have David Mayer on-line!
See this thread for details.
Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Class.forname and DriverManager

 
Raveendran Hs
Greenhorn
Posts: 23
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




How does DriverManager establishes the connection with the DB
i.e. DriverManager is from java.sql package how does it get the services from the above loaded driver class file

Can any one please tell me in detail.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a Driver class file is loaded by the ClassLoader, it is required to register itself with the DriverManager (using a static initialisation block)
This code block is ony called once, so it is OK to call Class.forName() multiple times without too much concern.
When the DriverManager is given a JDBC URL, it asks each of the Drivers that it knows whether they are able to accept a URL in that format. If the Driver says yes then the DriverManager asks it to create a connection which is then returned to the calling code.
 
Raveendran Hs
Greenhorn
Posts: 23
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ClassLoader, is required to register itself with the DriverManager (using a static initialisation block) .

I not able to understand this part. register means what?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
register means it must cal DriverManager.registerDriver() to tell the DriverManager that this Driver is available.
static initialisation is a section in a class that looks like this which gets executed once when the Class is loaded:

If you cause the class to be loaded then the message will be displayed:
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!