Oracle 數(shù)據(jù)庫(kù)中三種不同類型的JDBC驅(qū)動(dòng)
以下的文章主要介紹的是Oracle 數(shù)據(jù)庫(kù)里三種不同類型的JDBC驅(qū)動(dòng),我們大家都知道Oracle 中的jdbc驅(qū)動(dòng)主要有以下的三類,即,1、JDBC OCI: oci是Oracle call interface的縮寫,此驅(qū)動(dòng)類似于傳統(tǒng)的ODBC 驅(qū)動(dòng)。
因?yàn)樗枰狾racle Call Interface and Net8,所以它需要在運(yùn)行使用此驅(qū)動(dòng)的JAVA程序的機(jī)器上安裝客戶端軟件,其實(shí)主要是用到orcale客戶端里以dll方式提供的oci和服務(wù)器配置。
2、JDBC Thin: thin是for thin client的意思,這種驅(qū)動(dòng)一般用在運(yùn)行在WEB瀏覽器中的JAVA程序。它不是通過(guò)OCI or Net8,而是通過(guò)Java sockets進(jìn)行通信,是純java實(shí)現(xiàn)的驅(qū)動(dòng),因此不需要在使用JDBC Thin的客戶端機(jī)器上安裝orcale客戶端軟件,所以有很好的移植性,通常用在web開發(fā)中。
3、JDBC KPRB: 這種驅(qū)動(dòng)由直接存儲(chǔ)在數(shù)據(jù)庫(kù)中的JAVA程序使用,如Java Stored Procedures 、triggers、Database JSP's。因?yàn)槭窃诜?wù)器內(nèi)部使用,他使用默認(rèn)或當(dāng)前的會(huì)話連接來(lái)訪數(shù)據(jù)庫(kù),不需要用戶名密碼等,也不需要數(shù)據(jù)庫(kù)url。
在應(yīng)用開發(fā)的時(shí)候,通常是用前面兩種方式,下面是數(shù)據(jù)庫(kù)url的寫法:
- jdbc:Oracle :thin:@server ip: service
- jdbc:Oracle :oci:@service
看來(lái)oci的還更加簡(jiǎn)潔,ip可以省掉不寫了,這是因?yàn)閛ci驅(qū)動(dòng)通過(guò)客戶端的native java methods來(lái)?xiàng)l用c library方式來(lái)訪問(wèn)數(shù)據(jù)庫(kù)服務(wù)器,使用到了客戶端的net manager里的數(shù)據(jù)庫(kù)服務(wù)配置。
因?yàn)閛ci方式最終與數(shù)據(jù)庫(kù)服務(wù)器通信交互是用的c library庫(kù),理論上性能優(yōu)于thin方式,據(jù)說(shuō)主要是體現(xiàn)在blob字段的存取上。
開發(fā)Oracle 數(shù)據(jù)庫(kù)經(jīng)常用到的 pl sql dev使用的估計(jì)是oci方式,需要安裝客戶端,但也可以不安裝,但是要抽出其中的oci相關(guān)的dll即jar包、注冊(cè)環(huán)境變量、配置偵聽文件等。Oracle 在10g之后提供了精簡(jiǎn)客戶端,安裝的過(guò)程應(yīng)該包括上面的那些工作。
- How does one connect with the JDBC OCI Driver?
- One must have Net8 (SQL*Net) installed and working before attempting to use one of the OCI drivers.
- Code: [Copy to clipboard]
- import java.sql.*;
- class dbAccess {
- public static void main (String args []) throws SQLException
- {
- try {
- Class.forName ("Oracle .jdbc.driver.Oracle Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- Connection conn = DriverManager.getConnection
- ("jdbc:Oracle :oci8:@ORA1", "scott", "tiger");
- // or oci9 @Service, userid, password
- Statement stmt = conn.createStatement();
- ResultSet rset = stmt.executeQuery (
- "select BANNER from SYS.V_$VERSION"
- );
- while (rset.next())
- System.out.println (rset.getString(1)); // Print col 1
- stmt.close();
- }
- }
- How does one connect with the JDBC KPRB Driver?
- One can obtain a handle to the default or current connection
(KPRB driver) by calling the Oracle Driver.defaultConenction() method.
Please note that you do not need to specify a database URL,
username or password as you are already connected to a database session.
Remember not to close the default connection.
Closing the default connection might throw an exception in future releases of Oracle .- import java.sql.*;
- Code: [Copy to clipboard]
- class dbAccess {
- public static void main (String args []) throws SQLException
- {
- Connection conn = (new
- Oracle .jdbc.driver.Oracle Driver()).defaultConnection();
- Statement stmt = conn.createStatement();
- ResultSet rset = stmt.executeQuery (
- "select BANNER from SYS.V_$VERSION"
- );
- while (rset.next())
- System.out.println (rset.getString(1)); // Print col 1
- stmt.close();
- }
- }
以上的相關(guān)內(nèi)容就是對(duì)Oracle 數(shù)據(jù)庫(kù)中三種類型的JDBC驅(qū)動(dòng)的介紹,望你能有所收獲。
【編輯推薦】