日期:2014-05-20  浏览次数:20780 次

方法返回值是接口类型的对象。
JDBC 中DriverManager.getConnection(),调用的方法如下(从RT.JAR 反编译):

Java code
  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);实现了接口的一个对象!没有具体的实现类,为什么能实现一个对象呢?或者请告知具体的实现类是什么呢?


------解决方案--------------------
探讨
注册Connection 这个类么??