zoukankan      html  css  js  c++  java
  • 关于jdbc注冊驱动的那点事

    看到非常多人写jdbc连接工具类的时候,都会写到Class.forName()去显示载入类,一写错点点就会抛出ClassNotFoundException,关于显示载入类,究竟会不会产生作用呢?

    參考下Mysql Driver源代码:

    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()
    	}
    }

    再结合下jdk(1.6)帮助文档:

    Applications no longer need to explictly load JDBC drivers using Class.forName(). Existing programs which currently load JDBC drivers usingClass.forName() will continue to work without modification.

    明白指出不要显示载入类

    The DriverManager methods getConnection and  getDrivers have been enhanced to support the Java Standard Edition Service Provider mechanism. JDBC 4.0 Drivers must include the file META-INF/services/java.sql.Driver. This file contains the name of the JDBC drivers implementation ofjava.sql.Driver.  For example, to load the my.sql.Driver class, theMETA-INF/services/java.sql.Driver file would contain the entry: 

     my.sql.Driver
    

    JDBC 4.0 Drivers 必须包含 META-INF/services/java.sql.Driver 文件  (这里面存在的就是驱动名)

    结合以上,得出,凡是符合4.0规范的jar,是不须要进行显示注冊驱动的,就算你显示载入类,仅仅要类名不错,载入类为其它类依然能成功获取连接。

    演示样例:

  • 相关阅读:
    致 CODING 用户的元宵问候
    持续集成之理论篇
    基于 CODING 的 Spring Boot 持续集成项目
    使用 CODING 进行 Hexo 项目的持续集成
    使用 CODING 进行 Spring Boot 项目的集成
    三种前端模块化规范
    XSS和CSRF
    小问题填坑,关于obj.x和obj["x"]
    说一个闭包在实际开发中的应用
    关于return的分号自动插入问题
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4332188.html
Copyright © 2011-2022 走看看