Class aClass = Class.forName(xxx.xx.xx); Object anInstance = aClass.newInstance();
可以的,Class.forName("").newInstance()返回的是object
but there is some limit for this method to create instance that is your class constructor should no contain parameters, and you should case the instance manually
we just want to load the driver to jvm only, but not need to user the instance of driver, so call Class.forName(xxx.xx.xx) is enough, if you call Class.forName(xxx.xx.xx).newInstance(), the result will same as calling Class.forName(xxx.xx.xx), because Class.forName(xxx.xx.xx).newInstance() will load driver first, and then create instance, but the instacne you will never use in usual, so you need not to create it.
Object anInstance = aClass.newInstance();
that is your class constructor should no contain parameters, and you should case the instance manually
protected static Driver current;
public static Driver getDriver(){
return current;
}
}Class MyDriver extends Driver{
static{
Driver.current=new MyDriver();
}
MyDriver(){}
}用时:
Class.forName("MyDriver");
d=Driver.getDriver();
public class MyJDBCDriver implements Driver {
static {
DriverManager.registerDriver(new MyJDBCDriver());
}
}
所以我们在使用JDBC时只需要Class.forName(XXX.XXX);就可以了
.newInstance() 后才创建一个对象使用它有很多方法,你可以强制转换成你要的对象,
也可以使用 Apache 的 BeanUtils 设置/得到属性,你也可以通过它自己得到它有那些属性可以外部调用.