This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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 ]
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