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

演示样例:

最新文章

  1. 关于 Chrome 浏览器中 onresize 事件的 Bug
  2. PAT1053. Path of Equal Weight
  3. Spring的ControllerAdvice注解
  4. Java开发之多线程下载和断点续传
  5. Java同步问题面试参考指南
  6. Executor, ExecutorService 和 Executors 间的区别与联系
  7. 页面优化,谈谈重绘(repaint)和回流(reflow)
  8. java设定一个日期时间,加几分钟(小时或者天)后得到新的日期
  9. layui 子页面向父页面传值
  10. 爬虫:输入网页之后爬取当前页面的图片和背景图片,最后打包成exe
  11. 慕学在线网0.3_四个model
  12. C++ code:剩余串排列
  13. c#中的几种Dialog
  14. SVN详细配置与使用 ——一步步教会您使用
  15. 数据库ACID属性
  16. Linux init 命令
  17. 【Pyton】【小甲鱼】类和对象
  18. ubuntu16.04+pycharm+默认文件头注释
  19. js open窗口父子窗口操作
  20. Java变量的修饰符

热门文章

  1. xpath的文本获取
  2. Swift - 经纬度位置坐标与真实地理位置相互转化
  3. Good Bye 2013---B. New Year Present
  4. windows live writer插件说明文档(附录网盘地址)
  5. (摘录)MSMQ的简单介绍
  6. 【书评】RHCSA/RHCE Red Hat Linux 认证学习指南(第6版)EX200 &amp; EX300
  7. Java批量生成Mac地址到文件
  8. linux设备驱动程序注冊过程具体解释
  9. EL表达式(1)
  10. frame.bounds和center