JAVA驱动之接口驱动(上)


        Connection connection  = DriverManager.getConnection(URL,USER,PASSWORD        );
        Statement statement = connection.createStatement();
        String sql = "select 1 from dual;";
        ResultSet resultSet = statement.executeQuery(sql);
       while(resultSet.next()){
           System.out.println("获取查询结果:" + resultSet.getString(1));
       }
    }

对于上边的代码相信大家特别熟悉,利用JAVA连接数据库执行SQL语句的代码。对于数据库连接,java提供了一个统一接口java.sql.Driver接口,各大数据库运营商只要实现这个接口接口。

DriverManager.getConnection(URL,USER,PASSWORD)方法调用了Connection getConnection( String url, java.util.Properties info, Class caller),该方法中循环已经注册到registeredDrivers数组中的Driver实现类。

Driver实现类的注册是通过DriverManager静态块初始化时进行注册,源码如下:

//  DriverManager 静态块定义
static {
        loadInitialDrivers();
        println("JDBC DriverManager initialized");
    }
//  DriverManager loadInitialDrivers方法具体实现
private static void loadInitialDrivers() {
        String drivers;
        try {
            drivers = AccessController.doPrivileged(new PrivilegedAction() {
                public String run() {
                    return System.getProperty("jdbc.drivers");
                }
            });
        } catch (Exception ex) {
            drivers = null;
        }
        // If the driver is packaged as a Service Provider, load it.
        // Get all the drivers through the classloader
        // exposed as a java.sql.Driver.class service.
        // ServiceLoader.load() replaces the sun.misc.Providers()

        AccessController.doPrivileged(new PrivilegedAction() {
            public Void run() {

                ServiceLoader loadedDrivers = ServiceLoader.load(Driver.class);
                Iterator driversIterator = loadedDrivers.iterator();

                /* Load these drivers, so that they can be instantiated.
                 * It may be the case that the driver class may not be there
                 * i.e. there may be a packaged driver with the service class
                 * as implementation of java.sql.Driver but the actual class
                 * may be missing. In that case a java.util.ServiceConfigurationError
                 * will be thrown at runtime by the VM trying to locate
                 * and load the service.
                 *
                 * Adding a try catch block to catch those runtime errors
                 * if driver not available in classpath but it's
                 * packaged as service and that service is there in classpath.
                 */
                try{
                    while(driversIterator.hasNext()) {
                        driversIterator.next();
                    }
                } catch(Throwable t) {
                // Do nothing
                }
                return null;
            }
        });

        println("DriverManager.initialize: jdbc.drivers = " + drivers);

        if (drivers == null || drivers.equals("")) {
            return;
        }
        String[] driversList = drivers.split(":");
        println("number of Drivers:" + driversList.length);
        for (String aDriver : driversList) {
            try {
                println("DriverManager.Initialize: loading " + aDriver);
                Class.forName(aDriver, true,
                        ClassLoader.getSystemClassLoader());
            } catch (Exception ex) {
                println("DriverManager.Initialize: load failed: " + ex);
            }
        }
    }

其中该方法中有这样一段代码:


ServiceLoader loadedDrivers = ServiceLoader.load(Driver.class);
Iterator driversIterator = loadedDrivers.iterator();

ServiceLoader通过读取META-INF/services/下的配置文件来获取Driver的具体实现类。

JAVA驱动之接口驱动(上)_第1张图片

获取Driver实现类后,获取调用各数据库的具体实现类获取连接,执行SQL语句等。

描述的有欠缺,请多指正。

你可能感兴趣的:(JAVA驱动之接口驱动(上))