- Dustin
- 等级:


- 性别:
 - 文章: 25
- 积分: 148
- 来自: 广州/成都

|
使用JDBC时,我们都会很自然得使用下列语句:
java 代码
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
- String user = "";
- String psw = "";
- Connection con = DriverManager.getConnection(url,user,psw);
为什么说很自然呢,因为无论是网上还是书本教程上得例子都是这样的,而且程序也确实正常运行了,于是大家也就心安理得的找葫芦画瓢下去了。
一定要有这一句吗?不是的,我们完全可以用这样一句代替它:
java 代码
- com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
-
-
- String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
- String user = "";
- String psw = "";
- Connection con = DriverManager.getConnection(url,user,psw);
-
大家可能都看出个大概来了,我们只需要在调用DriverManager的getConnection方法之前,保证相应的Driver类已经被加载到jvm中,并且完成了类的初始化工作就行了,而具体是怎样实现这个功能却是没有讲究的。上面两种方法都可以实现这个功能,因此程序可以正常运行。注意了,如果我们进行如下操作,程序是不能正常运行的,因为这样仅仅使Driver类被装载到jvm中,却没有进行相应的初始化工作。
java 代码
- com.mysql.jdbc.Driver driver = null;
-
- ClassLoader cl = new ClassLoader();
- cl.loadClass("com.mysql.jdbc.Driver");
我们都知道JDBC是使用Bridge模式进行设计的,DriverManager就是其中的Abstraction,java.sql.Driver是Implementor,com.mysql.jdbc.Driver是Implementor的一个具体实现(请参考GOF的Bridge模式的描述)。大家注意了,前一个Driver是一个接口,后者却是一个类,它实现了前面的Driver接口。
Bridge模式中,Abstraction(DriverManager)是要拥有一个Implementor(Driver)的引用的,但是我们在使用过程中,并没有将Driver对象注册到DriverManager中去啊,这是怎么回事呢?jdk文档对Driver的描述中有这么一句:
When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager
哦,原来是com.mysql.jdbc.Driver在装载完后自动帮我们完成了这一步骤。源代码是这样的:
java 代码
- package com.mysql.jdbc
-
- public class Driver extends NonRegisteringDriver implements java.sql.Driver {
-
-
-
-
- static {
- t ry {
- java.sql.DriverManager.registerDriver(new Driver());
- } catch (SQLException E) {
- throw new RuntimeException("Can't register driver!");
- }
- }
-
-
-
-
-
-
-
-
- public Driver() throws SQLException {
-
- }
- }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
返回顶楼 |
|
|
- 歆渊
- 等级:


- 性别:
 - 文章: 424
- 积分: 1219

|
Class.forName(); 的好处在于你不用编译/运行时依赖于特定的 JDBC Driver 库, 也就减少了项目代码的依赖性. 而且也很容易改造成从配置文件读取JDBC配置, 从而可以在运行时动态更换JDBC/DB.
非M$系的书上和文章里多数这样写,应该因为是从读配置文件初始化JDBC的代码简化来的.
M$系的文章和教材经常是 Driver d = new XXXDriver(); 多半是保证Vendor-Lockin的传统习俗.
|
返回顶楼 |
|
|
- Dustin
- 等级:


- 性别:
 - 文章: 25
- 积分: 148
- 来自: 广州/成都

|
看来我的题目有点以偏概全了:)
complystill从宏观上道出了Class.forName()的作用,我主要是从微观上进行考量的
|
返回顶楼 |
|
|
- liangguanhui
- 等级:


- 性别:
 - 文章: 242
- 积分: 308

|
其实只要自己写一个Driver的代理就会对这个比较熟
|
返回顶楼 |
|
|
- jacshan
- 等级:


- 性别:
 - 文章: 71
- 积分: 162
- 来自: 珠海

|
Dustin 写道:
使用JDBC时,我们都会很自然得使用下列语句:
java 代码
- Class.forName("com.mysql.jdbc.Driver");
- String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
- String user = "";
- String psw = "";
- Connection con = DriverManager.getConnection(url,user,psw);
为什么说很自然呢,因为无论是网上还是书本教程上得例子都是这样的,而且程序也确实正常运行了,于是大家也就心安理得的找葫芦画瓢下去了。
一定要有这一句吗?不是的,我们完全可以用这样一句代替它:
java 代码
- com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
-
-
- String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
- String user = "";
- String psw = "";
- Connection con = DriverManager.getConnection(url,user,psw);
-
大家可能都看出个大概来了,我们只需要在调用DriverManager的getConnection方法之前,保证相应的Driver类已经被加载到jvm中,并且完成了类的初始化工作就行了,而具体是怎样实现这个功能却是没有讲究的。上面两种方法都可以实现这个功能,因此程序可以正常运行。注意了,如果我们进行如下操作,程序是不能正常运行的,因为这样仅仅使Driver类被装载到jvm中,却没有进行相应的初始化工作。
java 代码
- com.mysql.jdbc.Driver driver = null;
-
- ClassLoader cl = new ClassLoader();
- cl.loadClass("com.mysql.jdbc.Driver");
我们都知道JDBC是使用Bridge模式进行设计的,DriverManager就是其中的Abstraction,java.sql.Driver是Implementor,com.mysql.jdbc.Driver是Implementor的一个具体实现(请参考GOF的Bridge模式的描述)。大家注意了,前一个Driver是一个接口,后者却是一个类,它实现了前面的Driver接口。
Bridge模式中,Abstraction(DriverManager)是要拥有一个Implementor(Driver)的引用的,但是我们在使用过程中,并没有将Driver对象注册到DriverManager中去啊,这是怎么回事呢?jdk文档对Driver的描述中有这么一句:
When a Driver class is loaded, it should create an instance of itself and register it with the DriverManager
哦,原来是com.mysql.jdbc.Driver在装载完后自动帮我们完成了这一步骤。源代码是这样的:
java 代码
- package com.mysql.jdbc
-
- public class Driver extends NonRegisteringDriver implements java.sql.Driver {
-
-
-
-
- static {
- t ry {
- java.sql.DriverManager.registerDriver(new Driver());
- } catch (SQLException E) {
- throw new RuntimeException("Can't register driver!");
- }
- }
-
-
-
-
-
-
-
-
- public Driver() throws SQLException {
-
- }
- }
这里ClassLoader为什么不行,是类加载器的机制决定的 www.iteye.com/topic/25053
static块在什么时候执行?
1)当调用forName(String)载入class时执行,如果调用ClassLoader.loadClass并不会执行.forName(String,false,ClassLoader)时也不会执行.
2)如果载入Class时没有执行static块则在第一次实例化时执行.比如new ,Class.newInstance()操作
3)static块仅执行一次
|
返回顶楼 |
|
|
- huangpengxiao
- 等级:


- 性别:
 - 文章: 302
- 积分: 200
- 来自: 北京

|
嗯,受教
|
返回顶楼 |
|
|
- Godlikeme
- 等级:


- 性别:
 - 文章: 1159
- 积分: 744
- 来自: 北京

|
to 楼上的楼上 Class.forName和 ClassLoader.loadClass是两码事,一个实例化类,一个加载类,不好混到一起说。
|
返回顶楼 |
|
|