wood burning stoves*
The moose likes JDBC and the fly likes Driver loading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Driver loading" Watch "Driver loading" New topic
Author

Driver loading

Mike Yu
Ranch Hand

Joined: Nov 17, 2001
Posts: 175
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?


Thanks,<br />Mike
Shailesh Chandra
Ranch Hand

Joined: Aug 13, 2004
Posts: 1081

I think Answer is A


Gravitation cannot be held responsible for people falling in love ~ Albert Einstein
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Nov 17, 2001
Posts: 175
Hi David,

This is provided by JGuru and in Sun's Quizzes. Thank you for your help.
Mike Yu
Ranch Hand

Joined: Nov 17, 2001
Posts: 175
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

Joined: Mar 06, 2001
Posts: 13459

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Driver loading
 
Similar Threads
I am getting an error while loading struts-config.xml
Database Reconnection mechansim at resultset level
Why we require Class.forName() multiple times
problem connecting with the database
ClassNotFoundException.