There are many possible implementations of JDBC drivers. These implementations
are categorized as follows:
n Type 1 — drivers that implement the JDBC API as a mapping to another data
access API, such as ODBC. Drivers of this type are generally dependent on a
native library, which limits their portability. The JDBC-ODBC Bridge driver is an
example of a Type 1 driver.
n Type 2 — drivers that are written partly in the Java programming language and
partly in native code. These drivers use a native client library specific to the data
source to which they connect. Again, because of the native code, their portability
is limited.
n Type 3 — drivers that use a pure Java client and communicate with a middleware
server using a database-independent protocol. The middleware server then
communicates the client’s requests to the data source.
n Type 4 — drivers that are pure Java and implement the network protocol for a
specific data source. The client connects directly to the data source.
以上來自JDBC3.0的規(guī)范
Type1 用JDBC-ODBC bridge 來建立數(shù)據(jù)庫的connection,這種效率很一般,
public static Connection getConnectionbyBridge() {
Connection conn = null;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:abc", "cms", "cms");
System.out.println(conn.getTransactionIsolation());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
Typ2 是效率比較高的,部分用了jdbc的驅(qū)動,部分是要依賴數(shù)據(jù)庫的客戶端,比如ORACLE 10g OCI
public static Connection getConnectionOCI() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:oci:@127.0.0.1:1521:orcl", "cms", "cms");
System.out.println(conn.getAutoCommit());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
Type3:網(wǎng)絡(luò)協(xié)議驅(qū)動 這種驅(qū)動實際上是根據(jù)我們熟悉的三層結(jié)構(gòu)建立的. jdbc先把對數(shù)局庫的訪問請求傳遞給網(wǎng) 絡(luò)上的中間件服務(wù)器.
中間件服務(wù)器再把請求翻譯為符合數(shù)據(jù)庫規(guī)范的調(diào)用,再把這種調(diào)用
傳給數(shù)據(jù)庫服務(wù)器.如果中間件服務(wù)器也是用java開法的,那么在在中間層也可以使用1,2型 jdbc驅(qū)動程序作為訪問數(shù)據(jù)庫的方法.
網(wǎng)絡(luò)協(xié)議驅(qū)動---------中間件服務(wù)器------------數(shù)據(jù)庫Server
Type4 本地協(xié)議驅(qū)動
這種驅(qū)動直接把jdbc調(diào)用轉(zhuǎn)換為符合相關(guān)數(shù)據(jù)庫系統(tǒng)規(guī)范的請求.由于4型驅(qū)動寫的應(yīng)用可 以直接和數(shù)據(jù)庫服務(wù)器通訊.這種類型的驅(qū)動完全由java實現(xiàn),因此實現(xiàn)了平臺獨立性. 本地協(xié)議驅(qū)動---------數(shù)據(jù)庫Server
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn = DriverManager.getConnection(
"jdbc:oracle:thin:@127.0.0.1:1521:orcl", "cms", "cms");
System.out.println(conn.getAutoCommit());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}
對四種類型的jdbc驅(qū)動做了一個說明.那么它們適合那種類型的應(yīng)用開發(fā)呢?
Jdbc-odbc橋由于它的執(zhí)行效率不高,更適合做為開發(fā)應(yīng)用時的一種過度方案,或著對于初學(xué) 者了解jdbc編程也較適用.
對于那些需要大數(shù)據(jù)量操作的應(yīng)用程序則應(yīng)該考慮2,3,4型驅(qū)動.在intranet方面的應(yīng)用可以 考慮2型驅(qū)動,而且目前開發(fā)
的趨勢是使用純java.所以3,4型驅(qū)動也可以作為考慮對象. 至于基于internet方面的應(yīng)用就只有考慮3,4型驅(qū)動了.
因為3型驅(qū)動可以把多種數(shù)據(jù)庫驅(qū) 動都配置在中間層服務(wù)器.所以3型驅(qū)動最適合那種需要同時連接多個不同種類的數(shù)據(jù)庫, 并且對并發(fā)連接要求高的應(yīng)用.
4型驅(qū)動則適合那些連接單一數(shù)據(jù)庫的工作組應(yīng)用。
但是Typ2 和type1我認為很少會用到,type1 可移植,效率都不行,type2效率雖然高,但是可移植太差,只有3.4是最常用的,當(dāng)然大規(guī)模的分布式應(yīng)用3是很好的選擇,一般的企業(yè)應(yīng)用,我認為用4就很夠了,并且效率也高。