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"); }
它返回了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");
}
后来才发现在接口下有实现它的类,这样就好理解了.比如con.createStatement()这个方法就是调用实现它的类中的,多态不是有这种写法吗:Object obj = new String();这个实际上也是一样的啊