zoukankan      html  css  js  c++  java
  • JDBC_demo:java连接mysql过程

    1.任何数据库驱动程序都提供对java.sql.Driver接口的驱动类,mysql-connector-java-5.1.39-bin.jar中Driver

    package com.mysql.jdbc;
    
    import java.sql.DriverManager;
    import java.sql.SQLException;
    
    public class Driver extends NonRegisteringDriver
      implements java.sql.Driver
    {
      public Driver()
        throws SQLException
      {
      }
    
      static
      {
        try
        {
          DriverManager.registerDriver(new Driver());
        } catch (SQLException E) {
          throw new RuntimeException("Can't register driver!");
        }
      }
    }

    实际上Class.forName("com.mysql.jdbc.Driver")--->DriverManager.registerDriver(new Driver())。java.sql.DriverManager类是JDBC的管理层,负责管理JDBC驱动程序的基本服务。在DriverManager中Driver被包装为DriverInfo类,存储在:

       private final static CopyOnWriteArrayList<DriverInfo> registeredDrivers = new CopyOnWriteArrayList<>();

    2.Connection con= DriverManager.getConnection(url,"user","pass");遍历存储在registeredDrivers中的DriverInfo,调用各自的connect()

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

    3.com.mysql.jdbc.Driver继承NonRegisteringDriver,实际的connect在com.mysql.jdbc.NonRegisteringDriver.connect()中实现,方法返回类型为java.sql.Connection

    public java.sql.Connection connect(String url, Properties info)
        throws SQLException
      {
        if (url == null) {
          throw SQLError.createSQLException(Messages.getString("NonRegisteringDriver.1"), "08001", null);
        }
    
        if (StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:loadbalance://"))
          return connectLoadBalanced(url, info);
        if (StringUtils.startsWithIgnoreCase(url, "jdbc:mysql:replication://")) {
          return connectReplicationConnection(url, info);
        }
    
        Properties props = null;
    
        if ((props = parseURL(url, info)) == null) {
          return null;
        }
    
        if (!"1".equals(props.getProperty("NUM_HOSTS"))) {
          return connectFailover(url, info);
        }
        try
        {
          return ConnectionImpl.getInstance(host(props), port(props), props, database(props), url);
        }
        catch (SQLException sqlEx)
        {
          throw sqlEx;
        } catch (Exception ex) {
          SQLException sqlEx = SQLError.createSQLException(Messages.getString("NonRegisteringDriver.17") + ex.toString() + Messages.getString("NonRegisteringDriver.18"), "08001", null);
    
          sqlEx.initCause(ex);
    
          throw sqlEx;
        }
      }

    4.那么mysql-connector-java-5.1.39-bin.jar中具体是怎么执行sql的,怎么学习,有哪些书可以看呢???

  • 相关阅读:
    从句分析
    artDialog ( v 6.0.2 ) content 参数引入页面 html 内容
    Java实现 LeetCode 13 罗马数字转整数
    Java实现 LeetCode 13 罗马数字转整数
    Java实现 LeetCode 13 罗马数字转整数
    Java实现 LeetCode 12 整数转罗马数字
    Java实现 LeetCode 12 整数转罗马数字
    Java实现 LeetCode 12 整数转罗马数字
    Java实现 LeetCode 11 盛最多水的容器
    Java实现 LeetCode 11 盛最多水的容器
  • 原文地址:https://www.cnblogs.com/yunwuzhan/p/5872230.html
Copyright © 2011-2022 走看看