数据库驱动【获取connection对象】【篇幅一】

本文探讨了MySQL驱动8.0.13版本中获取Connection对象的过程,包括ConnectionUrl的acceptsUrl方法用于校验URL合法性,isConnectionStringSupported方法的正则校验,ConnectionUrlParser的解析过程,以及通过反射构造ConnectionUrl对象的细节。总结了从URL校验到ConnectionUrl实例创建的关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们不应该仅仅靠记忆来掌握技术,我们要靠我们自身的逻辑分析能力,理出一道逻辑线,总结出学习技术和解决技术问题的方法论,记忆知识帮助我们快速拿出东西解决问题,而方法论才是我们面临后续挑战的真正手段。

本次分析基于mysql驱动8.0.13版本

    public static Connection getConnection(String url,
        String user, String password) throws SQLException {
   
        java.util.Properties info = new java.util.Properties();

        if (user != null) {
   
            info.put("user", user);
        }
        if (password != null) {
   
            info.put("password", password);
        }

        return (getConnection(url, info, Reflection.getCallerClass()));
    }
package com.mysql.cj.jdbc;

.....
.....
.....

public class NonRegisteringDriver implements java.sql.Driver {
   

.....
.....
.....

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

        try {
   
            if (!ConnectionUrl.acceptsUrl(url)) {
   
                /*
                 * According to JDBC spec:
                 * The driver should return "null" if it realizes it is the wrong kind of driver to connect to the given URL. This will be common, as when the
                 * JDBC driver manager is asked to connect to a given URL it passes the URL to each loaded driver in turn.
                 */
                return null;
            }

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

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

                case FAILOVER_CONNECTION:
                    return FailoverConnectionProxy.createProxyInstance(conStr);

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

                default:<
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值