3. Driver 源码

news/2024/5/18 21:49:36 标签: jdbc, mysql

java.sql.Driver接口规定了Driver应该具有以下功能,重要的有三个acceptsURL判断jdbcUrl是否支持、创建一个连接、获取属性信息,三个主要接口。

下边以 NonRegisteringDriver​ 类的源码简单分析以下。

acceptsURL

acceptsURL(String url) 方法用来测试对指定的url,该驱动能否打开这个url连接。driver对自己能够连接的url会制定自己的协议,只有符合自己的协议形式的url才认为自己能够打开这个url,如果能够打开,返回true,反之,返回false;

Mysql-JDBC支持的驱动协议有:

connect

这里会根据URL 创建一个连接。一般是ConnectionImpl类型,下一篇文章会细说一下。其他类型的连接就先不看了。

public java.sql.Connection connect(String url, Properties info) throws SQLException {

    try {
        // 如果url是不自持的连接协议,则返回null
        if (!ConnectionUrl.acceptsUrl(url)) {
            return null;
        }

        ConnectionUrl conStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        switch (conStr.getType()) {
            case SINGLE_CONNECTION:
                return com.mysql.cj.jdbc.ConnectionImpl.getInstance(conStr.getMainHost());

            case FAILOVER_CONNECTION:
            case FAILOVER_DNS_SRV_CONNECTION:
                return FailoverConnectionProxy.createProxyInstance(conStr);

            case LOADBALANCE_CONNECTION:
            case LOADBALANCE_DNS_SRV_CONNECTION:
                return LoadBalancedConnectionProxy.createProxyInstance(conStr);

            case REPLICATION_CONNECTION:
            case REPLICATION_DNS_SRV_CONNECTION:
                return ReplicationConnectionProxy.createProxyInstance(conStr);

            default:
                return null;
        }

    } catch (UnsupportedConnectionStringException e) {
        // when Connector/J can't handle this connection string the Driver must return null
        return null;

    } catch (CJException ex) {
        throw ExceptionFactory.createException(UnableToConnectException.class,
                Messages.getString("NonRegisteringDriver.17", new Object[] { ex.toString() }), ex);
    }
}

SINGLE_CONNECTION 会创建一个 ConnectionImpl

    public static JdbcConnection getInstance(HostInfo hostInfo) throws SQLException {
        return new ConnectionImpl(hostInfo);
    }
getPropertyInfo

获取这些属性信息 HOST、PORT、DBNAME、USER、PASSWORD

@Override
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
    String host = "";
    String port = "";
    String database = "";
    String user = "";
    String password = "";

    if (!isNullOrEmpty(url)) {
        ConnectionUrl connStr = ConnectionUrl.getConnectionUrlInstance(url, info);
        if (connStr.getType() == Type.SINGLE_CONNECTION) {
            HostInfo hostInfo = connStr.getMainHost();
            info = hostInfo.exposeAsProperties();
        }
    }

    if (info != null) {
        host = info.getProperty(PropertyKey.HOST.getKeyName());
        port = info.getProperty(PropertyKey.PORT.getKeyName());
        database = info.getProperty(PropertyKey.DBNAME.getKeyName());
        user = info.getProperty(PropertyKey.USER.getKeyName());
        password = info.getProperty(PropertyKey.PASSWORD.getKeyName());
    }

    DriverPropertyInfo hostProp = new DriverPropertyInfo(PropertyKey.HOST.getKeyName(), host);
    hostProp.required = true;
    hostProp.description = Messages.getString("NonRegisteringDriver.3");

    DriverPropertyInfo portProp = new DriverPropertyInfo(PropertyKey.PORT.getKeyName(), port);
    portProp.required = false;
    portProp.description = Messages.getString("NonRegisteringDriver.7");

    DriverPropertyInfo dbProp = new DriverPropertyInfo(PropertyKey.DBNAME.getKeyName(), database);
    dbProp.required = false;
    dbProp.description = Messages.getString("NonRegisteringDriver.10");

    DriverPropertyInfo userProp = new DriverPropertyInfo(PropertyKey.USER.getKeyName(), user);
    userProp.required = true;
    userProp.description = Messages.getString("NonRegisteringDriver.13");

    DriverPropertyInfo passwordProp = new DriverPropertyInfo(PropertyKey.PASSWORD.getKeyName(), password);
    passwordProp.required = true;
    passwordProp.description = Messages.getString("NonRegisteringDriver.16");

    JdbcPropertySet propSet = new JdbcPropertySetImpl();
    propSet.initializeProperties(info);
    List<DriverPropertyInfo> driverPropInfo = propSet.exposeAsDriverPropertyInfo();

    DriverPropertyInfo[] dpi = new DriverPropertyInfo[5 + driverPropInfo.size()];
    dpi[0] = hostProp;
    dpi[1] = portProp;
    dpi[2] = dbProp;
    dpi[3] = userProp;
    dpi[4] = passwordProp;
    System.arraycopy(driverPropInfo.toArray(new DriverPropertyInfo[0]), 0, dpi, 5, driverPropInfo.size());

    return dpi;
}

http://www.niftyadmin.cn/n/5337871.html

相关文章

计算机组成原理 — 计算机系统概述

计算机系统概述 计算机系统简介 计算机系统 计算机系统由硬件和软件两大部分组成 计算机软硬件机器 微程序机器M0、实际机器M1 归属于硬件 &#xff1b;虚拟机M2、M3、M4归属于软件 计算机体系结构和计算机组成区别 计算机体系结构&#xff1a;程序员所见到的计算机系统的…

k8s-ingress一

Comfigmap&#xff1a;存储数据 Date&#xff1a; Key&#xff1a;value 挂载的方式&#xff0c;把配置信息传给容器 生产当中的yml文件很长&#xff1a; 有deployment 容器的探针 资源限制 Configmap 存储卷 Service Ingress K8s的对外服务&#xff0c;ingress Se…

ARM安装与项目结构

1. 安装环境 参考E:\peixunQianrushi\arm\ziliao\FS4412新版&#xff08;学生资料&#xff09;\环境相关资料 这边建议全部默认路径 安装注意事项&#xff1a; 1、在接下来的安装过程中&#xff0c;对于使用win10、win8的操作系统的用户&#xff0c;所有的安装请均以管理员身份…

第6章 继承与构造

继承是C类型演化的重要机制&#xff0c;在保留原有类的属性和行为的基础上&#xff0c;派生出的新类可以有某种程度的变异。 接受成员的新类称为派生类&#xff1b; 提供成员的类称为基类&#xff1b;基类是对若干个派生类的抽象&#xff0c;提取了派生类的公共特征&am…

光伏发电系统的MPPT控制策略研究

摘 要 面对全球日趋严重的能源危机问题&#xff0c;可再生能源的开发和利用得到了人们的高度重 视。其中辐射到地球太阳能资源是十分富饶的&#xff0c;绿色清洁的太阳能不会危害我们的生存 环境&#xff0c;因而受到了人们的广泛利用。光伏发电作为当前利用太阳能的主要方式…

『C++成长记』模板

&#x1f525;博客主页&#xff1a;小王又困了 &#x1f4da;系列专栏&#xff1a;C &#x1f31f;人之为学&#xff0c;不日近则日退 ❤️感谢大家点赞&#x1f44d;收藏⭐评论✍️ 目录 一、泛型编程 二、函数模板 &#x1f4d2;2.1函数模板概念 &#x1f4d2;2.2函数…

网络逻辑示意图工具

现代网络容纳了来自不同供应商的大量设备&#xff0c;支持一系列新技术&#xff0c;并跨越了分布在多个位置的边界&#xff0c;随着网络变得越来越复杂&#xff0c;网络管理员发现越来越难以跟踪网络领域的所有当代进步和发展&#xff0c;这使得网络管理比以往任何时候都更具挑…

【Java】HttpServlet类简单方法和请求显示

1、HttpServlet类简介&#x1f340; Servlet类中常见的三个类有&#xff1a;☑️HttpServlet类&#xff0c;☑️HttpServletRequest类&#xff0c;☑️HttpResponse类 &#x1f42c;其中&#xff0c;HttpServlet首先必须读取Http请求的内容。Servlet容器负责创建HttpServlet对…