• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Matthew Phillips
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks correct.
 
Michael Zalewski
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic