JDBC 笔记

Posted on 2021-08-31 09:21  金色的省略号  阅读(22)  评论(0编辑  收藏  举报

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

  Driver类静态加载,Class forName("com.mysql.jdbc.Driver")

  不需要显式的注册:静态加载自动调用registeredDrivers

    static {
        try {
            java.sql.DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
            throw new RuntimeException("Can't register driver!");
        }
    }
View Code
        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