jdbc 中四種type解釋 轉載
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的規范
Type1 用JDBC-ODBC bridge 來建立數據庫的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;
}
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:網絡協議驅動 這種驅動實際上是根據我們熟悉的三層結構建立的. jdbc先把對數局庫的訪問請求傳遞給網 絡上的中間件服務器. 中間件服務器再把請求翻譯為符合數據庫規范的調用,再把這種調用 傳給數據庫服務器.如果中間件服務器也是用java開法的,那么在在中間層也可以使用1,2型 jdbc驅動程序作為訪問數據庫的方法. 網絡協議驅動---------中間件服務器------------數據庫Server
Type4 本地協議驅動
這種驅動直接把jdbc調用轉換為符合相關數據庫系統規范的請求.由于4型驅動寫的應用可 以直接和數據庫服務器通訊.這種類型的驅動完全由java實現,因此實現了平臺獨立性. 本地協議驅動---------數據庫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-odbc橋由于它的執行效率不高,更適合做為開發應用時的一種過度方案,或著對于初學 者了解jdbc編程也較適用. 對于那些需要大數據量操作的應用程序則應該考慮2,3,4型驅動.在intranet方面的應用可以 考慮2型驅動,而且目前開發 的趨勢是使用純java.所以3,4型驅動也可以作為考慮對象. 至于基于internet方面的應用就只有考慮3,4型驅動了. 因為3型驅動可以把多種數據庫驅 動都配置在中間層服務器.所以3型驅動最適合那種需要同時連接多個不同種類的數據庫, 并且對并發連接要求高的應用. 4型驅動則適合那些連接單一數據庫的工作組應用。
但是Typ2 和type1我認為很少會用到,type1 可移植,效率都不行,type2效率雖然高,但是可移植太差,只有3.4是最常用的,當然大規模的分布式應用3是很好的選擇,一般的企業應用,我認為用4就很夠了,并且效率也高。