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
posted
0
That looks correct.
Matthew Phillips
Michael Zalewski
Ranch Hand
Joined: Apr 23, 2002
Posts: 168
posted
0
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
posted
0
Michael When u register a driver like u did, can we use this driver later on. ie can we call/use drvMySql? Thanks
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.
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