Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Driver loading

 
Mike Yu
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I got a question as follows:

Which of the following will not cause a JDBC driver to be loaded and registered with the DriverManager?
A. Class.forName(driverString);
B. new DriverClass();
C. Include driver name in jdbc.drivers system property
D. None of the above

I though the answer is B because I did not find any DriverClass API. However, I was told that the anwser is D. Can someone explain it to me?
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Answer is A
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When a JDBC Driver Class is loaded by the ClassLoader, it is required (via a static code block) to register itself with the DriverManager. A and B both accomplish this. C is a special mechanism for moving the Driver dclaration outside the code, but doesn't appear to used as often as A.

Therefore all 3 end up with the Driver class being loaded and registered with the DriverManager.

I'm hoping this isn't homework, can I ask where the question is from?
 
Mike Yu
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

This is provided by JGuru and in Sun's Quizzes. Thank you for your help.
 
Mike Yu
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

In your last post, you said "When a JDBC Driver Class is loaded by the ClassLoader, it is required (via a static code block) to register itself with the DriverManager".

Where is this static code block? Is it part of the Driver Class? And what is it?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Any basic Java book should have information on static and instance blocks. We're getting close to the point where you should ask a new question in the JiG(beginners) forum, but try this code:



When the Class is loaded (ie the first time a ClassLoader 'sees' this Class), it calls the static block once (and only ever once!) before it does anything else with the Class. In Drivers they register themselves with the DriverManager.

Hope this gets you started.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic