关于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,是不须要进行显示注冊驱动的,就算你显示载入类,仅仅要类名不错,载入类为其它类依然能成功获取连接。
演示样例:
关于jdbc注冊驱动的那点事的更多相关文章
- 迅为4412开发板Linux驱动教程——总线_设备_驱动注冊流程具体解释
视频下载地址: 驱动注冊:http://pan.baidu.com/s/1i34HcDB 设备注冊:http://pan.baidu.com/s/1kTlGkcR 总线_设备_驱动注冊流程具体解释 • ...
- 基于Servlet、JSP、JDBC、MySQL的一个简单的用户注冊模块(附完整源代码)
近期看老罗视频,做了一个简单的用户注冊系统.用户通过网页(JSP)输入用户名.真名和password,Servlet接收后通过JDBC将信息保存到MySQL中.尽管是个简单的不能再简单的东西,但麻雀虽 ...
- c++ 操作注冊表
1. 注冊表简单介绍 注冊表是为Windows NT和Windows95中全部32位硬件/驱动和32位应用程序设计的数据文件,用于存储系统和应用程序的设置信息.16位驱动在Winnt (W ...
- MyEclipse 8.0注冊码+原版下载_Java开发软件
MyEclipse是一个十分优秀的用于开发Java, J2EE的Eclipse插件集合,MyEclipse的功能很强大,支持也十分广泛,尤其是对各种开元产品的支持十分不错.MyEclipse眼下支持J ...
- Android应用程序注冊广播接收器(registerReceiver)的过程分析
前面我们介绍了Android系统的广播机制,从本质来说,它是一种消息订阅/公布机制,因此,使用这样的消息驱动模型的第一步便是订阅消息:而对Android应用程序来说,订阅消息事实上就是注冊广播接收器, ...
- linux设备驱动程序注冊过程具体解释
Linux的驱动程序注冊过程,大致分为两个步骤: 模块初始化 驱动程序注冊 以下以内核提供的演示样例代码pci-skeleton.c,具体说明一个pci设备驱动程序的注冊过程.其它设备的驱动代码注冊过 ...
- JSP_tomcat_mysql_注冊验证用户;
本文出自:http://blog.csdn.net/svitter 资源下载: github: git clone https://github.com/Svtter/JSP-tomcat-mysql ...
- Android Binder分析二:Natvie Service的注冊
这一章我们通过MediaPlayerService的注冊来说明怎样在Native层通过binder向ServiceManager注冊一个service,以及client怎样通过binder向Servi ...
- struts2学习笔记(三)—— 在用户注冊程序中使用验证框架
实现目标: 1.使用验证框架对用户注冊信息进行验证 2.验证username.password.邮箱不能为空 3.验证username.password长度 ...
随机推荐
- CodeForces 446B DZY Loves Modification
题意: k次操作 每次选择一行或一列 得到所选数字的和 并将所选数字同一时候减去p 问最多得到多少 思路: 重点在消除行列间的相互影响 因为每选一行全部列所相应的和都会-p 那么假设选了i次 ...
- Java学习——何为JNDI
曾记得在做机房收费系统的时候就接触到了API,由于它的功能非常强大,可是自己对它却不怎么了解.所以当时是又爱又怕.现在,一路走来才明确,事实上它就是一组接口.仅仅要我们去了解它就会发现.它事实上也没想 ...
- tomcat 后台启动设置
如果你环境变量配置对了且保证tomcat的bin目录下有如下三个文件,则你只需在cmd中运行 service install tomcat即可将tomcat添加的服务项中,然后在设置开机自动启动,则以 ...
- 解决android应用程序适用新老android系统版本方法
老的android系统不能运行高版本系统的新方法,为了解决这个问题: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { ...
- zipkin分布式链路追踪系统
基于zipkin分布式链路追踪系统预研第一篇 分布式服务追踪系统起源于Google的论文“Dapper, a Large-Scale Distributed Systems Tracing Inf ...
- MFC自绘(17篇)
http://www.cnblogs.com/lidabo/category/477377.html
- grub配置文件grub.conf详细说明
说明:只供学习交流 default行,是指grub启动时默认菜单项.0表示第一项,如果是多系统可以修改此选项改变默认光标停留位置. timeout行,是指菜单到自动启动系统前的停留时间,单位时间为se ...
- CSS中float属性和clear属性的一些笔记
在学习CSS的最后一部分内容中,float属性和clear属性比较难以用语言描述,因此在笔记本中无法准确的记录这两个属性的用法.所以在博客园上以图文的形式记录这两种属性的特征,以备以后查阅. 首先,定 ...
- 与众不同 windows phone (1) - Hello Windows Phone
原文:与众不同 windows phone (1) - Hello Windows Phone [索引页] [源码下载] 与众不同 windows phone (1) - Hello Windows ...
- form表单提交不成功提示
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...