對於大的項目當然我們都已經有了原有基本框架,但是對於一些新的技術探討的時候,我們還是直接調用Class.forName("com.mysql.jdbc.Driver")連接數據庫進行相關的測試
今天用HTTP大文件上傳斷點續傳控件發布-Xproer.HttpUploader5的時候發現這有點有點忘記,所有整理一些文檔進行做記錄
1 public class HttpUploaderDB { 2 String m_dbDriver ="com.mysql.jdbc.Driver"; 3 String m_dbUrl ="jdbc:mysql://127.0.0.1/xdb_files?useUnicode=true&characterEncoding=utf-8"; 4 5 /* 6 * 參數: 7 * sc = this.getServletContext() 8 * */ 9 public HttpUploaderDB(ServletContext sc) 10 { 11 } 12 13 public Connection GetCon() 14 { 15 Connection con = null; 16 17 try 18 { 19 Class.forName(this.m_dbDriver).newInstance();//加載驅動。 20 con = DriverManager.getConnection(this.m_dbUrl,"root","123456"); 21 } 22 catch (SQLException e) 23 { 24 // TODO Auto-generated catch block 25 e.printStackTrace(); 26 } catch (ClassNotFoundException e) { 27 // TODO Auto-generated catch block 28 e.printStackTrace(); 29 } catch (InstantiationException e) { 30 // TODO Auto-generated catch block 31 e.printStackTrace(); 32 } catch (IllegalAccessException e) { 33 // TODO Auto-generated catch block 34 e.printStackTrace(); 35 } 36 return con; 37 } 38 }
Class.forName("com.mysql.jdbc.Driver");的作用
- 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);
為什么說很自然呢,因為無論是網上還是書本教程上得例子都是這樣的,而且程序也確實正常運行了,於是大家也就心安理得的找葫蘆畫瓢下去了。
一定要有這一句嗎?不是的,我們完全可以用這樣一句代替它:
1 com.mysql.jdbc.Driver driver = new com.mysql.jdbc.Driver(); 2 //or: 3 //new com.mysql.jdbc.Driver(); 4 String url = "jdbc:mysql://127.0.0.1/test?useUnicode=true&characterEncoding=utf-8"; 5 String user = ""; 6 String psw = ""; 7 Connection con = DriverManager.getConnection(url,user,psw);
大家可能都看出個大概來了,我們只需要在調用DriverManager的getConnection方法之前,保證相應的Driver類已經被加載到 jvm中,並且完成了類的初始化工作就行了,而具體是怎樣實現這個功能卻是沒有講究的。上面兩種方法都可以實現這個功能,因此程序可以正常運行。注意了, 如果我們進行如下操作,程序是不能正常運行的,因為這樣僅僅使Driver類被裝載到jvm中,卻沒有進行相應的初始化工作。
java 代碼1 com.mysql.jdbc.Driver driver = null; 2 //or: 3 ClassLoader cl = new ClassLoader(); 4 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在裝載完后自動幫我們完成了這一步驟。源代碼是這樣的:
1 package com.mysql.jdbc 2 3 public class Driver extends NonRegisteringDriver implements java.sql.Driver { 4 // ~ Static fields/initializers 5 // --------------------------------------------- // 6 // Register ourselves with the DriverManager 7 // 8 static { 9 t ry { 10 java.sql.DriverManager.registerDriver(new Driver()); 11 } catch (SQLException E) { 12 throw new RuntimeException("Can't register driver!"); 13 } 14 } 15 // ~ Constructors 16 // ----------------------------------------------------------- 17 /** 18 * Construct a new driver and register it with DriverManager 19 * 20 * @throws SQLException 21 * if a database error occurs. 22 */ 23 public Driver() throws SQLException { 24 // Required for Class.forName().newInstance() 25 } 26 }