参考Class类的方法:
newInstance
public Object newInstance()//用类构造一个该类的对象
                   throws InstantiationException,
                          IllegalAccessException
Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized. 
If there is a security manager, this method first calls the security manager's checkMemberAccess method with this and Member.PUBLIC as its arguments. If the class is in a package, then this method also calls the security manager's checkPackageAccess method with the package name as its argument. Either of these calls could result in a SecurityException. forName//用类名返回一个Class对象
public static Class forName(String className)
                     throws ClassNotFoundException
Returns the Class object associated with the class or interface with the given string name. Invoking this method is equivalent to: 
  Class.forName(className, true, currentLoader)
 
where currentLoader denotes the defining class loader of the current class. 
For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread:    Class t = Class.forName("java.lang.Thread")
 
A call to forName("X") causes the class named X to be initialized. 

解决方案 »

  1.   

    这可以动态的生成类对象,例如,你可以动态的配置类名,然后根据类名构造对象,只能通过该方法,因为你程序中不知道是那个类,所以不能new了。
    Class class = Class.forName("com.xxx.xxx");
    Object obj = class.newInstance();
    就可以得到对象实例了。
      

  2.   

    在你的代码里,不用newInstance 也没什么问题.
      

  3.   

    是有来生成该类的一个实例,Class class = Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
    Object obj = class.newInstance();当然在该例中,不要Object obj = class.newInstance();也是正确的,因为
    你用Connection conn= DriverManager.getConnection(url,user,password); 连接数据源,它会根据你加载的驱动程序类也就是
    在com.microsoft.jdbc.sqlserver.SQLServerDriver自动连接数据源的!