Can we Load more then one Drivers at once?

Q1. Can we load more the one Driver thru Class.forName() method?
Q2. What is the difference if we are using Class.forName()method and DriverManager.registerDriver().
Which one is better?

You can load multiple drivers using the Class.forName() method.

The drivers could be for the same database or for different databases.

When you invoke Class.forName(), you, I mean the driver, are/is actually doing two things:

1) create an instance of the driver which is being loaded

2) register this instance of the driver with the DriverManager using the DriverManager.registerDriver() method.

In order to use DriverManager.registerDriver you need a reference to a Driver instance. One way to get a reference to a driver instance is using the getDrivers method of the DriverManager class. But all the Driver instances you get by invoking this method are already registered with the Drivermanager, so this is of no use. The 'other way' is to create an instance of the Driver like this:

Driver d = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(d);

Now, you tell me which one is better? ;)

P.S: Its always better to use Class.forName(drivername).newInstance() than using just Class.forName(drivername). (Yeah there is a duplicate instance). Every driver has code in a static initializer block which registers itself with the DriverManager. A few JVM implementations do not execute the static initializer block untill an instance of the class is created.

Vinay Binny

Related:

Java Books
Java Certification, Programming, JavaBean and Object Oriented Reference Books

Return to : Java Programming Hints and Tips

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.
The site www.erpgreat.com is not affiliated with or endorsed by any company listed at this site.
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk.
 The content on this site may not be reproduced or redistributed without the express written permission of
www.erpgreat.com or the content authors.