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

    演示样例:

  • 相关阅读:
    小白开学Asp.Net Core 《一》
    小白开学Asp.Net Core 开篇
    分享一个.NET平台开源免费跨平台的大数据分析框架.NET for Apache Spark
    微信支付退款中发现的一个问题
    发布mvc遇到的HTTP错误 403.14-Forbidden解决办法
    English--元音
    开发工具--浅谈Git
    开发工具--搭建python环境
    开发工具--PyCharm
    English--介词省略句型与总结
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4332188.html
Copyright © 2011-2022 走看看