那什么意思呢,?
Connection con=....;
Connection不是接口吗,也不是类啊?

解决方案 »

  1.   

    因为你的con = DriverManager.getConnection(utils.GetConnectString(),"",""); 
    它返回了implements java.sql.Connection接口的实际类啊  //  Worker method called by the public getConnection() methods.
        private static synchronized Connection getConnection(
    String url, java.util.Properties info, ClassLoader callerCL) throws SQLException {
    if(url == null) {
        throw new SQLException("The url cannot be null", "08001");
    }
        
    println("DriverManager.getConnection(\"" + url + "\")");
        
    if (!initialized) {
        initialize();
    } // Walk through the loaded drivers attempting to make a connection.
    // Remember the first exception that gets raised so we can reraise it.
    SQLException reason = null;
    for (int i = 0; i < drivers.size(); i++) {
        DriverInfo di = (DriverInfo)drivers.elementAt(i);
          
        // If the caller does not have permission to load the driver then 
        // skip it.
        if ( getCallerClass(callerCL, di.driverClassName ) != di.driverClass ) {
    println("    skipping: " + di);
    continue;
        }
        try {
    println("    trying " + di);
    Connection result = di.driver.connect(url, info);
    if (result != null) {
        // Success!
        println("getConnection returning " + di);
        return (result);
    }
        } catch (SQLException ex) {
    if (reason == null) {
        reason = ex;
    }
        }
    }
        
    // if we got here nobody could connect.
    if (reason != null)    {
        println("getConnection failed: " + reason);
        throw reason;
    }
        
    println("getConnection: no suitable driver");
    throw new SQLException("No suitable driver", "08001");
        }
      

  2.   

    你最开始注册驱动时不是传了一个字符串进去吗,Class.forName("className");这其实就是一个实现了Driver接口的类,当你调用DriverManager.getConnection(url)时,DriverManager将这个url传给已经注册过所有的Driver对象,看是否有哪一个驱动即Driver 实现类能够解吸这个url,假如有能够解吸的话,那么调用这个Driver实现类的getConnection方法,而不同的Driver的getconnection方法实现肯定是不同的,但是他们都返回的是实现的cnnection接口的实现类,所以当你调用getconnection方法时,你实际在调用你的Class.forName传如的类的getconnection方法
      

  3.   

    我原来也遇到类似的问题,跟楼主一样的疑惑.
       后来才发现在接口下有实现它的类,这样就好理解了.比如con.createStatement()这个方法就是调用实现它的类中的,多态不是有这种写法吗:Object obj = new String();这个实际上也是一样的啊