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.
Comments
Post a Comment