使用JDBC时,我们都会很自然得使用下列语句:
一定要有这一句吗?不是的,我们完全可以用这样一句代替它:
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在装载完后自动帮我们完成了这一步骤。源代码是这样的:
1Class.forName("com.mysql.jdbc.Driver");
2String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
3String user = "";
4String psw = "";
5Connection con = DriverManager.getConnection(url,user,psw);
为什么说很自然呢,因为无论是网上还是书本教程上得例子都是这样的,而且程序也确实正常运行了,于是大家也就心安理得的找葫芦画瓢下去了。 2String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
3String user = "";
4String psw = "";
5Connection con = DriverManager.getConnection(url,user,psw);
一定要有这一句吗?不是的,我们完全可以用这样一句代替它:
1com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
2//or:
3//new com.mysql.jdbc.Driver();
4String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
5String user = "";
6String psw = "";
7Connection con = DriverManager.getConnection(url,user,psw);
大家可能都看出个大概来了,我们只需要在调用DriverManager的getConnection方法之前,保证相应的Driver类已经被加载到
jvm中,并且完成了类的初始化工作就行了,而具体是怎样实现这个功能却是没有讲究的。上面两种方法都可以实现这个功能,因此程序可以正常运行。注意了,
如果我们进行如下操作,程序是不能正常运行的,因为这样仅仅使Driver类被装载到jvm中,却没有进行相应的初始化工作。2//or:
3//new com.mysql.jdbc.Driver();
4String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8";
5String user = "";
6String psw = "";
7Connection con = DriverManager.getConnection(url,user,psw);
1com.mysql.jdbc.Driver driver = null;
2//or:
3ClassLoader cl = new ClassLoader();
4cl.loadClass("com.mysql.jdbc.Driver");
我们都知道JDBC是使用Bridge模式进行设计的,DriverManager就是其中的Abstraction,java.sql.Driver是
Implementor,com.mysql.jdbc.Driver是Implementor的一个具体实现(请参考GOF的Bridge模式的描
述)。大家注意了,前一个Driver是一个接口,后者却是一个类,它实现了前面的Driver接口。 2//or:
3ClassLoader cl = new ClassLoader();
4cl.loadClass("com.mysql.jdbc.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在装载完后自动帮我们完成了这一步骤。源代码是这样的:
package com.mysql.jdbc
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()
}
}
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()
}
}