JDBC 中DriverManager.getConnection(),调用的方法如下(从RT.JAR 反编译):  private static Connection getConnection(String paramString, Properties paramProperties, ClassLoader paramClassLoader)
    throws SQLException
  {
    synchronized (DriverManager.class)
    {
      if (paramClassLoader == null)
        paramClassLoader = Thread.currentThread().getContextClassLoader();
    }
    if (paramString == null)
      throw new SQLException("The url cannot be null", "08001");
    println("DriverManager.getConnection(\"" + paramString + "\")");
    ??? = null;
    Iterator localIterator = registeredDrivers.iterator();
    while (localIterator.hasNext())
    {
      DriverInfo localDriverInfo = (DriverInfo)localIterator.next();
      if (isDriverAllowed(localDriverInfo.driver, paramClassLoader))
        try
        {
          println("    trying " + localDriverInfo.driver.getClass().getName());
          Connection localConnection = localDriverInfo.driver.connect(paramString, paramProperties);
          if (localConnection != null)
          {
            println("getConnection returning " + localDriverInfo.driver.getClass().getName());
            return localConnection;
          }
        }
        catch (SQLException localSQLException)
        {
          if (??? == null)
            ??? = localSQLException;
        }
      else
        println("    skipping: " + localDriverInfo.getClass().getName());
    }
    if (??? != null)
    {
      println("getConnection failed: " + ???);
      throw ???;
    }
    println("getConnection: no suitable driver found for " + paramString);
    throw new SQLException("No suitable driver found for " + paramString, "08001");
  }
问题是: Connection 是个接口,Connection localConnection = localDriverInfo.driver.connect(paramString, paramProperties);实现了接口的一个对象!没有具体的实现类,为什么能实现一个对象呢?或者请告知具体的实现类是什么呢?

解决方案 »

  1.   

    这是多态最典型的写法,方法应返回基类的引用
    具体的实现类,你看 localDriverInfo.driver.connect方法不就好了
      

  2.   


    可是在API 中无localDriverInfo 这个接口,只有driver接口,并且driver.connect()方法中并没有实现connect()方法,其中DRIVER 接口代码如下:
    package java.sql;import java.util.Properties;
    import java.util.logging.Logger;public abstract interface Driver
    {
      public abstract Connection connect(String paramString, Properties paramProperties)
        throws SQLException;  public abstract boolean acceptsURL(String paramString)
        throws SQLException;  public abstract DriverPropertyInfo[] getPropertyInfo(String paramString, Properties paramProperties)
        throws SQLException;  public abstract int getMajorVersion();  public abstract int getMinorVersion();  public abstract boolean jdbcCompliant();  public abstract Logger getParentLogger()
        throws SQLFeatureNotSupportedException;
    所以就更看不明白了,请指点!
      

  3.   

    connection是由具体的数据库厂商去实现的,因为各个数据库oracle,mysql,sqlserver等内部怎么连接都不一样的,所以jdcb只提供接口,各个厂商去具体实现。你要看实现,你就看各个数据库连接怎么实现的
      

  4.   

    楼主,这个事情,本质上就是JVM提供了一组接口和工厂方法。然后各厂商开发的JDBC驱动程序,会在JVM中自动注册自己的具体实现类而已。由于你直接看不到这个过程,所以感觉上会有点玄幻。
    但总的来说,凡是返回Interface的对象,你都可以直接查看所返回对象的真实类型:
      System.out.println(retobj.getClass());
    就知道它实际上是个啥了。
      

  5.   

    注册Connection  这个类么??
      

  6.   


    不是,Connection是个接口而已。是注册该接口的具体实现类。比如:
      Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3737/test", "root", "root");
      System.out.println(cn.getClass());
    显示结果:
      class com.mysql.jdbc.JDBC4Connection