Class.forName("")与DriverManager.registerDriver()有区别吗?
我是菜鸟希望说详细点

解决方案 »

  1.   

    Class.forName 表示加载一个类。加载一个类并没有实例化这个类,在加载类的同时会初始化这个类中所有的静态成员、静态块和静态方法。根据 JDBC 规范,Driver 的实现类必须在静态块中使用 DriverManager.registerDriver() 方法将自己注册到驱动管理器中去。这也就是需要执行 Class.forName 的原因。当然了,加载 JDBC 驱动还有其他的方法,Class.forName 只是其中之一。其他还有:* 在系统属性将 jdbc.drivers 属性的值设为 Driver 的实现类名* 实现 JDBC 4.0 规范的驱动以后不再需要 Class.forName 或者是 jdbc.drivers 了,其采用 Service Provider 方式加载驱动。因为 JDBC 4 规定 JDBC 的 jar 包必须在 META-INF/services/java.sql.Driver 文件中写出 Driver 的实现类。需要支持 JDBC 4 新特性的功能需要在 JDK 6 中运行,不过所有的 JDBC 驱动都是向下兼容的,因此 JDBC 4 的驱动在JDK 5 及以下版本无法使用 JDBC 4 中的新特性。MySQL 支持 JDBC 4 驱动版本是 Connector/J 5.0.0-beta 及以后版本
    Oracle 支持 JDBC 4 驱动版本是 ojdbc6.jar
    MS SQL Server 支持 JDBC 4 驱动版本是 Microsoft SQL Server JDBC Driver 2.0
      

  2.   

    下面这段代码是 MySQL JDBC 的 Driver 实现类的源代码,可以看一下其静态块中的内容:/*
     Copyright  2002-2004 MySQL AB, 2008 Sun Microsystems
     All rights reserved. Use is subject to license terms.  The MySQL Connector/J is licensed under the terms of the GPL,
      like most MySQL Connectors. There are special exceptions to the
      terms and conditions of the GPL as it is applied to this software,
      see the FLOSS License Exception available on mysql.com.  This program is free software; you can redistribute it and/or
      modify it under the terms of the GNU General Public License as
      published by the Free Software Foundation; version 2 of the
      License.  This program is distributed in the hope that it will be useful,  
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.  You should have received a copy of the GNU General Public License
      along with this program; if not, write to the Free Software
      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
      02110-1301 USA */
    package com.mysql.jdbc;import java.sql.SQLException;/**
     * The Java SQL framework allows for multiple database drivers. Each driver
     * should supply a class that implements the Driver interface
     * 
     * <p>
     * The DriverManager will try to load as many drivers as it can find and then
     * for any given connection request, it will ask each driver in turn to try to
     * connect to the target URL.
     * 
     * <p>
     * It is strongly recommended that each Driver class should be small and
     * standalone so that the Driver class can be loaded and queried without
     * bringing in vast quantities of supporting code.
     * 
     * <p>
     * When a Driver class is loaded, it should create an instance of itself and
     * register it with the DriverManager. This means that a user can load and
     * register a driver by doing Class.forName("foo.bah.Driver")
     * 
     * @see org.gjt.mm.mysql.Connection
     * @see java.sql.Driver
     * @author Mark Matthews
     * @version $Id$
     */
    public class Driver extends NonRegisteringDriver implements java.sql.Driver {
        // ~ Static fields/initializers
        // ---------------------------------------------    //
        // Register ourselves with the DriverManager
        //
        static {
            try {
                java.sql.DriverManager.registerDriver(new Driver());
            } catch (SQLException E) {
                throw new RuntimeException("Can't register driver!");
            }
        }    // ~ Constructors
        // -----------------------------------------------------------    /**
         * Construct a new driver and register it with DriverManager
         * 
         * @throws SQLException
         *             if a database error occurs.
         */
        public Driver() throws SQLException {
            // Required for Class.forName().newInstance()
        }
    }
      

  3.   

    刚才看了一下 Oracle 的 ojdbc5.jar 也支持 JDBC 4 呵呵