Two Laptop Bag
The moose likes JDBC and Relational Databases and the fly likes What is the format for Class.forName(...) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "What is the format for Class.forName(...)" Watch "What is the format for Class.forName(...)" New topic

What is the format for Class.forName(...)

Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
I'm using mysql driver: mysql-connector-java-2.0.14
Is this line of code correct for this particular driver?:
[ January 31, 2003: Message edited by: Rob Petterson ]

Rob Petterson
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
That looks correct.

Matthew Phillips
Michael Zalewski
Ranch Hand

Joined: Apr 23, 2002
Posts: 168
I bet the following will work for you:

The trick is you must call registerDriver() in order for the URL to be mapped to the correct driver.
Mohammed Ali

Joined: Feb 02, 2003
Posts: 4
When u register a driver like u did, can we use this driver later on. ie can we call/use drvMySql?
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Michael Zalewski:
I bet the following will work for you...

No I don't think so. The Class.forName() (without the 'newInstance' added) is the preferred way to do it.
The 'newInstance' was only required by older versions of Java where a Class was not loaded until an instance was created.
A static block in the Driver Class should allow it to register itself with the DriverManager, you shoudn't have to do it yourself.
from the Sun JDBC Tutorial:
You do not need to create an instance of a driver and register it with the DriverManager because calling Class.forName will do that for you automatically. If you were to create your own instance, you would be creating an unnecessary duplicate, but it would do no harm.

David O'Meara

Joined: Mar 06, 2001
Posts: 13459

I was concerned that the behaviour of registering the same driver twice was not documented in the API so I had a quick look at the DriverManager source code. It's available with the JDK if you want to look for yourself.
registerDriver essentially adds the required Driver to a Vector, so adding it twice with the same Driver instance will only cause a slight memory overhead, the second copy should be completely hidden by the first and never be called.
Unless the deregisterDriver call assumes a Driver only exists once... Back to the source code...
Yep, deregisterDriver assumes the Driver has only been registered once, therefore the above code will cause deregisterDriver to make the DriverManager unstable since in effect the DRiver will still exist after it has been deregistered. You'd have to call deregister twice.
I'm recommending that you use Class.forName() ONLY
This is my opinion only, but it is the preferred way to register Drivers and the other method could cause unforseen consequences.
I agree. Here's the link:
subject: What is the format for Class.forName(...)
It's not a secret anymore!