JDBC驱动类型的区别

来源:互联网 发布:python 生成临时文件 编辑:程序博客网 时间:2024/04/28 01:53

转帖至: http://log-cd.javaeye.com/blog/358065

 

 

 

一、jdbc-odbc桥
   
    JDBC-ODBC桥加ODBC驱动程序,JavaSoft桥产品利用ODBC驱动程序提供JDBC访问。注意, 必须将ODBC二进制代码(许多情况下还包括数据库客户机代码)加载到使用该驱动程序的每个客户机上。这种类型的驱动程序最适合于企业网(这种网络上客户机的安装不是主要问题),或者是用Java编写的三层结构的应用程序服务器代码。
    Application--->JDBC-ODBC Bridge(Type1 jdbc driver)---->JDBC-ODBC Library--->ODBC Driver-->Database
    只要本地机装有相关的odbc驱动那么采用jdbc-odbc桥几乎可以访问所有的数据库,jdbc- odbc方法对于客户端已经具备odbc driver的应用还是可行的。适用于快速的原型系统,没有提供JDBC驱动的数据库如Access。
     但是,由于jdbc-odbc先调用 odbc再由odbc去调用本地数据库接口访问数据库.所以,执行效率比较低,对于那些大数据量 存取的应用是不适合的.而且,这种方法要求客户端必须安装odbc 驱动,所以对于基于 internet ,intranet的应用也是不合适的。

二、本地Api驱动:JAVA to Native API

    这种类型的驱动程序把客户机API上的JDBC调用转换为Oracle、Sybase、Informix、DB2或其它DBMS的调用。注意,象桥驱动程序一样,这种类型的驱动程序要求将某些二进制代码加载到每台客户机上。
    Application--->JDBC Driver(Type2 jdbc driver)---->Native Database library---->Database利用开发商提供的本地库来直接与数据库通信。本地api驱动直接把jdbc调用转变为数据库的标准调用再去访问数据库。

三、网络协议驱动:Java to net

    这种驱动程序将JDBC转换为与DBMS无关的网络协议,这种协议又被某个服务器转换为一种DBMS协议。这种网络服务器中间件能够将它的纯Java客户机连接到多种不同的数据库上。所用的具体协议取决于提供者。通常,这是最为灵活的JDBC驱动程序。有可能所有这种解决方案的提供者都提供适合于Intranet用的产品。为了使这些产品也支持Internet访问,它们必须处理Web所提出的安全性、通过防火墙的访问等方面的额外要求。
   
    Application--->Jdbc Driver(Type3 jdbc driver)----->java middleware--->JDBC Driver---->Database
    jdbc先把对数局库的访问请求传递给网络上的中间件服务器. 中间件服务器再把请求翻译为符合数据库规范的调用,再把这种调用传给数据库服务器.如果中间件服务器也是用java开发的,那么在在中间层也可以使用1,2型 jdbc驱动程序作为访问数据库的方法。
     由于这种驱动是基于server的.所以,它不需要在客户端加载数据库厂商提供的代码库.而且他在执行效率和可升级性方面是比较好的.因为大部分功能实现都在server端,所以这种驱动 可以设计的很小,可以非常快速的加载到内存中.但是,这种驱动在中间件层仍然需要有配置 其它数据库驱动程序,并且由于多了一个中间层传递数据,它的执行效率还不是最好。

四、本地协议驱动:JAVA to native dababase

    这种类型的驱动程序将JDBC调用直接转换为DBMS所使用的网络协议。这将允许从客户机机器上直接调用DBMS服务器,是Intranet访问的一个很实用的解决方法。
    Application--->Jdbc driver(type4 jdbc driver)----->database engine--->database
    这种驱动直接把jdbc调用转换为符合相关数据库系统规范的请求.由于4型驱动写的应用可以直接和数据库服务器通讯.这种类型的驱动完全由java实现,因此实现了平台独立性。最高的性能,通过自己的本地协议直接与数据库引擎通信,具备在Internet装配的能力。

附Oracle JDBC Drivers is illustrated :
Oracle provides four different types of JDBC drivers, for use in different deployment scenarios. The 10.1.0 drivers can access Oracle 8.1.7 and higher. While all Oracle JDBC drivers are similar, some features apply only to JDBC OCI drivers and some apply only to the JDBC Thin driver.
      JDBC OCI client-side driver: This is a JDBC Type 2 driver that uses Java native methods to call entrypoints in an underlying C library. That C library, called OCI (Oracle Call Interface), interacts with an Oracle database. The JDBC OCI driver requires an Oracle client installation of the same version as the driver.
      The use of native methods makes the JDBC OCI driver platform specific. Oracle supports Solaris, Windows, and many other platforms. This means that the Oracle JDBC OCI driver is not appropriate for Java applets, because it depends on a C library.
       Starting from 10.1.0, the JDBCOCI driver is available for install with the OCI Instant Clientfeature, which does not require a complete Oracle client-installation.Please refer to Oracle Call Interface for more information.
       JDBC Thin client-side driver: This is a JDBC Type 4 driver that uses Java to connect directly to Oracle. It implements Oracle's SQL*Net Net8 and TTC adapters using its own TCP/IP based Java socket implementation. The JDBCThin driver does not require Oracle client software to be installed,but does require the server to be configured with a TCP/IP listener.
      Because it is written entirely in Java, this driver is platform-independent. The JDBCThin driver can be downloaded into any browser as part of a Javaapplication. (Note that if running in a client browser, that browsermust allow the applet to open a Java socket connection back to theserver.)
     JDBC Thin server-side driver: This is another JDBC Type 4driver that uses Java to connect directly to Oracle. This driver isused internally within the Oracle database. This driver offers the samefunctionality as the client-side JDBC Thin driver (above), but runs inside an Oracle database and is used to access remote databases. 
     Because it is written entirely in Java, this driver isplatform-independent. There is no difference in your code between usingthe Thin driver from a client application or from inside a server.

连接方式有以下几种:

Oralce provides four types of JDBC driver.

  Thin Driver, a 100% Java driver for client-side use without anOracle installation, particularly with applets. The Thin driver type isthin. To connect user scott with password tiger to a database with SID(system identifier) orcl through port 1521 of host myhost, using theThin driver, you would write :
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@myhost:1521:orcl", "scott", "tiger");

OCI Driver for client-side use with an Oracle client installation.The OCI driver type is oci. To connect user scott with password tigerto a database with SID (system identifier) orcl through port 1521 ofhost myhost, using the OCI driver, you would write :
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci:@myhost:1521:orcl", "scott", "tiger");

Note that you can also specify the database by a TNSNAMES entry.You can find the available TNSNAMES entries listed in the filetnsnames.ora on the client computer from which you are connecting. Forexample, if you want to connect to the database on host myhost as userscott with password tiger that has a TNSNAMES entry of MyHostString,enter:
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:@MyHostString","scott","tiger");

If your JDBC client and Oracle serverare running on the same machine, the OCI driver can use IPC(InterProcess Communication) to connect to the database instead of anetwork connection. An IPC connection is much faster than a networkconnection.
Connection conn = DriverManager.getConnection
("jdbc:oracle:oci8:@","scott","tiger");

Server-Side Thin Driver, which is functionally the same as theclient-side Thin driver, but is for code that runs inside an Oracleserver and needs to access a remote server, including middle-tierscenarios. The Server-Side Thin driver type is thin and there is nodifference in your code between using the Thin driver from a clientapplication or from inside a server.
Server-Side Internal Driver for code that runs inside the targetserver, that is, inside the Oracle server that it must access. TheServer-Side Internal driver type is kprb and it actually runs within adefault session. You are already "connected". Therefore the connectionshould never be closed.
To access the default connection, write:
DriverManager.getConnection("jdbc:oracle:kprb:");
or:
DriverManager.getConnection("jdbc:default:connection:");

You can also use the Oracle-specific defaultConnection() method of the OracleDriver class which is generally recommended:
OracleDriver ora = new OracleDriver();
Connection conn = ora.defaultConnection();

Note: You are no longer required to register the OracleDriver classfor connecting with the Server-Side Internal driver, although there isno harm in doing so. This is true whether you are using getConnection()or defaultConnection() to make the connection.
Any user name or password you include in the URL string isignored in connecting to the server default connection. TheDriverManager.getConnection() method returns a new Java Connectionobject every time you call it. Note that although the method is notcreating a new physical connection (only a single implicit connectionis used), it is returning a new object.
Again, when JDBC code is runninginside the target server, the connection is an implicit data channel,not an explicit connection instance as from a client. It should neverbe closed.

原创粉丝点击