File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes loading driver Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "loading driver" Watch "loading driver" New topic
Author

loading driver

kooskuntla venkat
Greenhorn

Joined: Sep 10, 2005
Posts: 11
what is the difference with Class.forname and DriverManager
Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
Hi kooskuntla venkat,

The following line of code
Class.forName("MyDriver");
will load the Driver with name MyDriver and also registers it with DriverManager by calling DriverManager.registerDriver().There is no need for you to register that driver explicitly by calling DriverManager.registerDriver() because Class.forName is doing both the things for you.

Regards Sujith
kooskuntla venkat
Greenhorn

Joined: Sep 10, 2005
Posts: 11
Originally posted by Sujith Kanaparthi:
Hi kooskuntla venkat,

The following line of code
Class.forName("MyDriver");
will load the Driver with name MyDriver and also registers it with DriverManager by calling DriverManager.registerDriver().There is no need for you to register that driver explicitly by calling DriverManager.registerDriver() because Class.forName is doing both the things for you.

Regards Sujith
kooskuntla venkat
Greenhorn

Joined: Sep 10, 2005
Posts: 11
hello sujith
is there any memory management issue arises while loading driver with class.forname and drivermanager.registerdriver
from venkat
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

If you use DriverManager explicitly, you will register the Driver twice and there will be two instances held by the DriverManager. Ithe grand scheme of tings the memory is negligable (unless you keep registering it over and over and over again) so you should avid doing it, but otherwise don't worry about it.

Dave
Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
Ithe grand scheme of tings the memory is negligable (unless you keep registering it over and over and over again)

Hi David,

I hope Every Driver class has a static block of that creates a new Instance of the Diver and registers it with DriverManager.Since it is a static block that is being executed, even if try to register it once again the code is not going to execute once again.
David please comment on this .
Regards
Sujith
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

You can call Class.forName(...) as many times as you like and the Class will only be loaded once, and as a side effect the Class will only register itself with the DriverManager once.

BUT (and this was true the last time I checked the DriverManager source, but feel free to see if they have fixed it) calling DriverManager.registerDriver(...) will cause the Driver to be registered every time the line is called.

So the following code:

will register the Driver twice the first time it is called, and once each time it is called after that. I'm pretty sure this is not what you intend.
Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
Hi David,
I Thank you for the clarification that you have provided on what happens when DriverManager.registerDriver(..) is called.
Regards
Sujith
 
wood burning stoves
 
subject: loading driver