你是否正在寻找关于class.forname的内容?让我把最内涵的东西奉献给你:
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 cast the instance manually.
Class Driver{
protected static Driver current;
public static Driver getDriver(){
return current;
}
}
Class MyDriver extends Driver{
static{
Driver.current=new MyDriver();
}
MyDriver(){}
}
用时:
class.forname("MyDriver");
Driver d=Driver.getDriver();
有的jdbc连接的写法里是Class.forName(xxx.xx.xx);而有一些:class.forname(xxx.xx.xx).newInstance(),为什么会有这两种写法呢?
class.forname(xxx.xx.xx) 返回的是一个类,
.newInstance() 后才创建一个对象
class.forname(xxx.xx.xx);的作用是要求JVM查找并加载指定的类,也就是说JVM会执行该类的静态代码段
在JDBC规范中明确要求这个Driver类必须向DriverManager注册自己,即任何一个JDBC Driver的Driver类的代码都必须类似如下:
public class MyJDBCDriver implements Driver {
static {
DriverManager.registerDriver(new MyJDBCDriver());
}
}
所以我们在使用JDBC时只需要class.forname(XXX.XXX);就可以了
以上就是关于class.forname的全部内容,相信你一定会非常满意,。
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-18631-1.html
我才看到因为我还没有每天刷微博的习惯
e租宝利息这么高吗