对jdbc流程进行源码分析


            mysql</groupId>
            mysql-connector-java</artifactId>
            runtime</scope>
        </dependency>

package com.example.dtest.jdbc;

import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Component
public class JdbcConnection {

    public static Connection getConn() {
        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://49.235.125.47:3306/dblogs";
        String username = "root";
        String password = "123456";
        Connection conn = null;
        try {
            Class.forName(driver); //classLoader,加载对应驱动
            conn = (Connection) DriverManager.getConnection(url, username, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }

}

第一步我们是要进行驱动的加载:
String driver = “com.mysql.jdbc.Driver”;
Class.forName(driver); //classLoader,加载对应驱动
我们点进去看看:注意这里进入的是Driver类:
对jdbc流程进行源码分析_第1张图片
在meaven中找到要注册的驱动类:
对jdbc流程进行源码分析_第2张图片

public class Driver extends com.mysql.cj.jdbc.Driver
看到它继承了com.mysql.cj.jdbc.Driver类,那又找到com.mysql.cj.jdbc.Driver类看看:
对jdbc流程进行源码分析_第3张图片
对jdbc流程进行源码分析_第4张图片
看到这个类在构造前的编译时期会执行会执行静态代码块:
对jdbc流程进行源码分析_第5张图片
会将自己注册进DriverManager类:
DriverManager.registerDriver(new Driver());
对jdbc流程进行源码分析_第6张图片
对jdbc流程进行源码分析_第7张图片
在这里插入图片描述
再看连接:
conn = (Connection) DriverManager.getConnection(url, username, password);
恰好使用的是DriverManager类:
对jdbc流程进行源码分析_第8张图片

 private static Connection getConnection(
        String url, java.util.Properties info, Class<?> caller) throws SQLException {
        /*
         * When callerCl is null, we should check the application's
         * (which is invoking this class indirectly)
         * classloader, so that the JDBC driver class outside rt.jar
         * can be loaded from here.
         */
        ClassLoader callerCL = caller != null ? caller.getClassLoader() : null;
        if (callerCL == null || callerCL == ClassLoader.getPlatformClassLoader()) {
            callerCL = Thread.currentThread().getContextClassLoader();
        }

        if (url == null) {
            throw new SQLException("The url cannot be null", "08001");
        }

        println("DriverManager.getConnection(\"" + url + "\")");

        ensureDriversInitialized();

        // Walk through the loaded registeredDrivers attempting to make a connection.
        // Remember the first exception that gets raised so we can reraise it.
        SQLException reason = null;

        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());
            }

        }

        // if we got here nobody could connect.
        if (reason != null)    {
            println("getConnection failed: " + reason);
            throw reason;
        }

        println("getConnection: no suitable driver found for "+ url);
        throw new SQLException("No suitable driver found for "+ url, "08001");
    }


}

看这个getConnection方法:
对jdbc流程进行源码分析_第9张图片
用驱动得到连接!!!

你可能感兴趣的:(jdbc,jdbc)