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的规范
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;
}
Typ2 是效率比较高的,部分用了jdbc的驱动,部分是要依赖数据库的客户端,比如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:网络协议驱动 这种驱动实际上是根据我们熟悉的三层结构建立的. 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驱动做了一个说明.那么它们适合那种类型的应用开发呢?
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就很够了,并且效率也高。