jdbc连接驱动器的注册加载

来源:互联网 发布:java点击按钮触发事件 编辑:程序博客网 时间:2024/06/05 15:43

一,jdbc连接驱动器的注册加载

 

Java代码  收藏代码
  1. Class.forName("com.mysql.jdbc.Driver");  

     当以上类被装载时执行以下程序

 

Java代码  收藏代码
  1. package com.mysql.jdbc;  
  2.   
  3. import java.sql.SQLException;  
  4. public class Driver extends NonRegisteringDriver implements java.sql.Driver {  
  5.       
  6.        //执行这个静态代码块  
  7.     static {  
  8.         try {//注册mysql实现的驱动类  
  9.             java.sql.DriverManager.registerDriver(new Driver());  
  10.         } catch (SQLException E) {  
  11.             throw new RuntimeException("Can't register driver!");  
  12.         }  
  13.     }  
  14.   
  15.     public Driver() throws SQLException {  
  16.         // Required for Class.forName().newInstance()  
  17.     }  
  18. }  

 

 

进入java.sql.DriverManager.registerDirver(new Driver());中的功能实现

 

Java代码  收藏代码
  1.    public static synchronized void registerDriver(java.sql.Driver driver)  
  2. throws SQLException {  
  3. if (!initialized) {  
  4.            //初始化动作在下面作详解  
  5.     initialize();  
  6. }  
  7.        //用来存储驱动器信息  
  8. DriverInfo di = new DriverInfo();  
  9.   
  10. di.driver = driver;  
  11. di.driverClass = driver.getClass();  
  12. di.driverClassName = di.driverClass.getName();  
  13.   
  14. // Not Required -- drivers.addElement(di);  
  15.        //用于加入驱动的集合  
  16. writeDrivers.addElement(di);   
  17. println("registerDriver: " + di);  
  18.   
  19. /* 用于读取驱动的集合 */  
  20. readDrivers = (java.util.Vector) writeDrivers.clone();  
  21. span style="white-space: pre;"> </span>//用以上两个集合达到读写分离的状态,由 Vector 的 iterator 和 listIterator 方法所返回的迭代器是<em>快速失败的</em>  
  22.    }  

   二,驱动器的初始化操作

 

Java代码  收藏代码
  1. static void initialize() {  
  2.     if (initialized) {  
  3.         return;  
  4.     }  
  5.    //private static boolean initialized = false;静态全局变量,只初始化一次  
  6.     initialized = true;  
  7.     loadInitialDrivers();  
  8.     println("JDBC DriverManager initialized");  
  9. }  

 实现初始化操作

Java代码  收藏代码
  1. private static void loadInitialDrivers() {  
  2.         String drivers;  
  3.       
  4.         try {//得到系统属性jdbc.drivers对应驱动的驱动名称,使用了JAVA的安全许可  
  5.         drivers = (String) java.security.AccessController.doPrivileged(  
  6.         new sun.security.action.GetPropertyAction("jdbc.drivers"));  
  7.         } catch (Exception ex) {  
  8.             drivers = null;  
  9.         }  
  10.           
  11.         // If the driver is packaged as a Service Provider,  
  12.         // load it.  
  13.           
  14.         // Get all the drivers through the classloader   
  15.         // exposed as a java.sql.Driver.class service.  
  16.       
  17.      DriverService ds = new DriverService();  
  18.   
  19.      // Have all the privileges to get all the   
  20.      // implementation of java.sql.Driver  
  21.      java.security.AccessController.doPrivileged(ds);         
  22.               
  23.          println("DriverManager.initialize: jdbc.drivers = " + drivers);  
  24.         if (drivers == null) {  
  25.             return;  
  26.         }  
  27.         while (drivers.length() != 0) {  
  28.             int x = drivers.indexOf(':');  
  29.             String driver;  
  30.             if (x < 0) {  
  31.                 driver = drivers;  
  32.                 drivers = "";  
  33.             } else {  
  34.                 driver = drivers.substring(0, x);  
  35.                 drivers = drivers.substring(x+1);  
  36.             }  
  37.             if (driver.length() == 0) {  
  38.                 continue;  
  39.             }  
  40.             try {  
  41.                 println("DriverManager.Initialize: loading " + driver);  
  42.                 Class.forName(driver, true,  
  43.                   ClassLoader.getSystemClassLoader());  
  44.             } catch (Exception ex) {  
  45.                 println("DriverManager.Initialize: load failed: " + ex);  
  46.             }  
  47.         }  
  48.     }  

 以上初始化代码分析

  内部类对象,创建此对象时,它会从系统服务中加载驱动

 

Java代码  收藏代码
  1. DriverService ds = new DriverService();  

   代码如下:

   class DriverService implements java.security.PrivilegedAction {

Java代码  收藏代码
  1.         Iterator ps = null;  
  2.     public DriverService() {};  
  3.         public Object run() {  
  4.   
  5.        //从系统服务中加载驱动  
  6.     ps = Service.providers(java.sql.Driver.class);  
  7.   
  8.     /* Load these drivers, so that they can be instantiated.  
  9.      * It may be the case that the driver class may not be there 
  10.          * i.e. there may be a packaged driver with the service class 
  11.          * as implementation of java.sql.Driver but the actual class 
  12.          * may be missing. In that case a sun.misc.ServiceConfigurationError 
  13.          * will be thrown at runtime by the VM trying to locate  
  14.      * and load the service. 
  15.          *  
  16.      * Adding a try catch block to catch those runtime errors 
  17.          * if driver not available in classpath but it's  
  18.      * packaged as service and that service is there in classpath. 
  19.      */  
  20.           
  21.     try {  
  22.            while (ps.hasNext()) {  
  23.                ps.next();//遍历所有的驱动  
  24.            } // end while  
  25.     } catch(Throwable t) {  
  26.         // Do nothing  
  27.     }  
  28.         return null;  
  29.     } //end run  
  30.   
  31. }   

 

 

Java代码  收藏代码
  1. //找到所有的拥有权限的java.sql.Driver的实现  
  2. java.security.AccessController.doPrivileged(ds);     


转自http://wuquanyin1011.iteye.com/blog/1409455

 
原创粉丝点击