栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JDBC加载原理

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JDBC加载原理

大学的时候就学过了JDBC,当时一知半解,现在来搞懂以下详细的注册的原理。

JavaWeb的老师教了以下这句代码,说是注册驱动,但是原理是什么是没有细说的。

Class.forName("com.mysql.jdbc.Driver");

查看jdbc jar包的代码,如下

public class Driver extends com.mysql.cj.jdbc.Driver {
    public Driver() throws SQLException {
    }

    static {
        System.err.println("Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.");
    }
}

再找到

com.mysql.cj.jdbc.Driver这个包

代码如下所示:

public class Driver extends NonRegisteringDriver implements java.sql.Driver {
    public Driver() throws SQLException {
    }

    static {
        try {
            DriverManager.registerDriver(new Driver());
        } catch (SQLException var1) {
            throw new RuntimeException("Can't register driver!");
        }
    }
}

可以看到,使用了static代码块,在加载类(Class.forName)的时候,就对jdbc驱动进行了注册。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/721750.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号