wood burning stoves 2.0*
The moose likes JDBC 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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "What is the format for Class.forName(...)" Watch "What is the format for Class.forName(...)" New topic
Author

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?:
Class.forName("com.mysql.jdbc.Driver").newInstance();
[ January 31, 2003: Message edited by: Rob Petterson ]

Rob Petterson
SCJP
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
Greenhorn

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

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.

Dave
David O'Meara
Rancher

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.
Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: What is the format for Class.forName(...)
 
Similar Threads
How to connect MySQL DB from JSP?
Loading Oracle JDBC Driver
creating database connection
Loading Oracle JDBC Driver
Not able to connect to Mysql through servlet