Class.forName跟New的比较
Class.forName和New的比较public class MyJDBCDriver implements Driver {static {DriverManager.register
Class.forName和New的比较
public class MyJDBCDriver implements Driver {static {DriverManager.registerDriver(new MyJDBCDriver());}}?
所以我们在使用JDBC时只需要Class.forName(XXX.XXX);就可以了
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.
在JDBC驱动中,有一块静态代码,也叫静态初始化块,它执行的时间是当class调入到内存中就执行(你可以想像成,当类调用到内存后就执行一个方法)。所以很多人把jdbc driver调入到内存中,再实例化对象是没有意义的。
?