java.sql.DriverManager

來源:互聯網
上載者:User

Applications no longer need to explictly load JDBC drivers using Class.forName()

. Existing programs which currently load JDBC drivers using Class.forName()
will continue to work without modification.

 

When the method getConnection
is called,the 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 classloader<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 /> }<br /> }

 

so when "getConnection()" over loading method has been invoked in Applications, the JDBC dirver would be loaded!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.