JDBC 笔记

  1、DriverManager:注册driver,其实是把这个driver存到了DriverManager里面的一个CopyOnWriteArrayList<DriverInfo>类型的变量里,名为registeredDrivers,然后在你java.sql.DriverManager.getConnection(String, Properties, Class<?>)里取出注册的链接。

registeredDrivers代码

JDBC 笔记
    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }
View Code JDBC 笔记
        for(DriverInfo aDriver : registeredDrivers) {
            // If the caller does not have permission to load the driver then
            // skip it.
            if(isDriverAllowed(aDriver.driver, callerCL)) {
                try {
                    println("    trying " + aDriver.driver.getClass().getName());
                    Connection con = aDriver.driver.connect(url, info);
                    if (con != null) {
                        // Success!
                        println("getConnection returning " + aDriver.driver.getClass().getName());
                        return (con);
                    }
                } catch (SQLException ex) {
                    if (reason == null) {
                        reason = ex;
                    }
                }

            } else {
                println("    skipping: " + aDriver.getClass().getName());
            }

        }
View Code

 

上一篇:11.常见对象(Object(类)中得 hashCode,getClass,toString,equals,clone方法)


下一篇:Java错题集锦3