因为jdbc这样调用就已经把类加载到DriverManager里面了
如果我们要动态地加栽一个类肯定会使用这个返回值的

解决方案 »

  1.   

    Class aClass = Class.forName(xxx.xx.xx);
    Object anInstance = aClass.newInstance();
      

  2.   

    可以的,Class.forName("").newInstance()返回的是object
      

  3.   

    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
      

  4.   

    说道newInstance(),这里我也有一点疑问就是有的jdbc连接数据库的写法里是Class.forName(xxx.xx.xx);而有一些:Class.forName(xxx.xx.xx).newInstance(),请问为什么会有这两种写法呢?
      

  5.   

    jFresH_MaN(The answer is ......) had told you the answer
      

  6.   

    如果这样写只要Class.forName(xxx.xx.xx);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");
    d=Driver.getDriver();
      

  7.   

    dyhml(VirusCamp)请问能解释一下吗我看你的代码好像在说明内部是如何构造的,而在jdbc编程中何时使用Class.forName(xxx.xx.xx),何时使用Class.forName(xxx.xx.xx).newInstance()?呢,应该怎么区分二者?
      

  8.   

    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);就可以了
      

  9.   

    Class.forName(xxx.xx.xx) 返回的是一个类,
    .newInstance() 后才创建一个对象使用它有很多方法,你可以强制转换成你要的对象,
    也可以使用 Apache 的 BeanUtils 设置/得到属性,你也可以通过它自己得到它有那些属性可以外部调用.
      

  10.   

    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.
      

  11.   

    如果有时间的话,可以去看看jdbc的source,比如mysql的。从代码上看,在driver类的里面,有一块静态代码,也叫静态初始化块,它执行的时间是当class调入到内存中就执行(你可以想像成,当类调用到内存后就执行一个方法)。所以很多人把jdbc driver调入到内存中,再实例化对象是没有意义的。