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);实现了接口的一个对象!没有具体的实现类,为什么能实现一个对象呢?或者请告知具体的实现类是什么呢?
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);实现了接口的一个对象!没有具体的实现类,为什么能实现一个对象呢?或者请告知具体的实现类是什么呢?
解决方案 »
- 控件的属性,如何修改啊? jTable
- [求助]java long与Long比较
- JAVA有运算符重载吗?
- 谁知道用jFreeChart生成两张图表,并且在同一界面上显示他们?
- java技术交流站 群号:2303735
- 你知道如何用java创建可执行文件吗?
- 大家援助一下呀!!!前贴《100分求教jtable问题???(lhdjk ) 》100分我一定落实
- (1)socket能不能在applet中运用?(2)应用程序中能不能播放声音?谢谢!
- 在座的有多少有 SCJP 证书的?
- 如何判断一个字符串是否能够转换成为整数,如将"1"转换成1,注意,我问的是如何判断。谢谢!!
- Comparator排序题
- javaBean 和 Model 的关系?
具体的实现类,你看 localDriverInfo.driver.connect方法不就好了
可是在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;
所以就更看不明白了,请指点!
但总的来说,凡是返回Interface的对象,你都可以直接查看所返回对象的真实类型:
System.out.println(retobj.getClass());
就知道它实际上是个啥了。
不是,Connection是个接口而已。是注册该接口的具体实现类。比如:
Connection cn = DriverManager.getConnection("jdbc:mysql://localhost:3737/test", "root", "root");
System.out.println(cn.getClass());
显示结果:
class com.mysql.jdbc.JDBC4Connection