Java. SQL. drivermanager

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.