Applications no longer need to explictly load JDBC drivers usingClass.forName()
. Existing programs which currently load JDBC drivers usingClass.forName()
Will continue to work without modification.
When the MethodgetConnection
Is called,DriverManager
Will attempt to locate a suitable driver from amongst those loaded at Initialization
And those loaded explicitly using the same classloader as the current applet or application.
The "public static connection getconnection ()" method will invoke "Private Static connection getconnection (string URL, Java. util. properties info, classloader callercl) throws sqlexception ", and then finally invoke" Private Static void loadinitialdrivers ()"
To implement JDBC driver loading.
Sourcecode:
Private Static void loadinitialdrivers () {<br/> string drivers; </P> <p> try {<br/> drivers = (string) Java. security. accesscontroller. doprivileged (<br/> New sun. security. action. getpropertyaction ("JDBC. drivers "); <br/>} catch (exception ex) {<br/> drivers = NULL; <br/>}</P> <p> // if the driver is packaged as a service provider, <br/> // load it. </P> <p> // get all the drivers through the classloa Der <br/> // exposed as a java. SQL. driver. class service. </P> <p> driverservice DS = new driverservice (); <br/> // have all the privileges to get all the <br/> // Implementation of Java. SQL. driver <br/> JAVA. security. accesscontroller. doprivileged (DS); </P> <p> println ("drivermanager. initialize: JDBC. drivers = "+ drivers); <br/> If (drivers = NULL) {<br/> return; <br/>}< br/> while (drivers. length ()! = 0) {<br/> int x = drivers. indexof (':'); <br/> string driver; <br/> If (x <0) {<br/> driver = drivers; <br/> drivers = ""; <br/>}else {<br/> driver = drivers. substring (0, x); <br/> drivers = drivers. substring (x + 1); <br/>}< br/> If (driver. length () = 0) {<br/> continue; <br/>}< br/> try {<br/> println ("drivermanager. initialize: loading "+ driver); <br/> class. forname (driver, true, <br/> classloader. getsystemclassloader (); <br/>} catch (exception ex) {<br/> println ("drivermanager. initialize: Load failed: "+ ex); <br/>}< br/>}
So when "getconnection ()" over loading method has been invoked in applications, the JDBC dirver wocould be loaded!