使用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);
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
java 代碼
- com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver();
- //or:
- //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);
com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver(); //or: //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中,卻沒有進行相應的初始化工做。 mysql
java 代碼
- com.mysql.jdbc.Driver driver = null;
- //or:
- ClassLoader cl = new ClassLoader();
- cl.loadClass("com.mysql.jdbc.Driver");
com.mysql.jdbc.Driver driver = null; //or: 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在裝載完後自動幫咱們完成了這一步驟。源代碼是這樣的: sql
java 代碼
- package com.mysql.jdbc
-
- public class Driver extends NonRegisteringDriver implements java.sql.Driver {
- // ~ Static fields/initializers
- // --------------------------------------------- //
- // Register ourselves with the DriverManager
- //
- static {
- t ry {
- 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()
- }
- }
package com.mysql.jdbc public class Driver extends NonRegisteringDriver implements java.sql.Driver { // ~ Static fields/initializers // --------------------------------------------- // // Register ourselves with the DriverManager // static { t ry { 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() } }