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,是不须要进行显示注冊驱动的,就算你显示载入类,仅仅要类名不错,载入类为其它类依然能成功获取连接。

    演示样例:

  • 相关阅读:
    16进制与10进制
    npm模块管理器
    Vue2+VueRouter2+webpack 构建项目实战(四)接通api,先渲染个列表
    cross-env使用笔记
    webpack 运行提示“The ‘mode‘ option has not been set”的原因和解决方法
    cnpm install -S 与cnpm install -D (dependencies和devDependencies的区别)
    Webpack基础学习
    webpack入门——webpack的安装与使用
    npm init 之package.json
    入门 Webpack,看这篇就够了
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4332188.html
Copyright © 2011-2022 走看看