jadbc oracle clob,maven之構建jdbc連oracle的四種方式實例和注冊驅動源碼淺析

還是先來看看參考吧

好的  還是看看 oracle官網的描述

Java Database Connectivity (JDBC) is a Java standard that provides the interface for connecting from Java to relational databases. The JDBC standard is defined by Sun Microsystems and implemented through the standard java.sql interfaces. This allows individual providers to implement and extend the standard with their own JDBC drivers. JDBC is based on the X/Open SQL Call Level Interface (CLI). JDBC 4.0 complies with the SQL 2003 standard.

恩 就不翻譯了 很簡單 關鍵是 翻譯了 也不太到位 哎

oracle 按照功能與用途  支持了四種類型的驅動

In addition to supporting the standard JDBC application programming interfaces (APIs), Oracle drivers have extensions to support Oracle-specific data types and to enhance performance.

Oracle provides the following JDBC drivers:

■Thin driver

It is a pure Java driver used on the client-side, without an Oracle client installation. It can be used with both applets and applications.

■Oracle Call Interface (OCI) driver

It is used on the client-side with an Oracle client installation. It can be used only with applications.

■Server-side Thin driver

It is functionally similar to the client-side Thin driver. However, it is used for code that runs on the database server and needs to access another session either on the same server or on a remote server on any tier.

■Server-side internal driver

It is used for code that runs on the database server and accesses the same session. That is, the code runs and accesses data from a single Oracle session.

還有一處 是oracle的jdbc的javadoc的 覺得也不錯 貼出來 看看

Oralce provides four types of JDBC driver.

Thin Driver, a 100% Java driver for client-side use without an Oracle installation, particularly with applets. The Thin driver type is thin. To connect user scott with password tiger to a database with SID (system identifier) orcl through port 1521 of host myhost, using the Thin 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 tiger to a database with SID (system identifier) orcl through port 1521 of host 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 file tnsnames.ora on the client computer from which you are connecting. For example, if you want to connect to the database on host myhost as user scott 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 server are running on the same machine, the OCI driver can use IPC (InterProcess Communication) to connect to the database instead of a network connection. An IPC connection is much faster than a network connection.

Connection conn = DriverManager.getConnection

("jdbc:oracle:oci8:@","scott","tiger");

Server-Side Thin Driver, which is functionally the same as the client-side Thin driver, but is for code that runs inside an Oracle server and needs to access a remote server, including middle-tier scenarios. The Server-Side Thin driver type is thin and there is no difference in your code between using the Thin driver from a client application or from inside a server.

Server-Side Internal Driver for code that runs inside the target server, that is, inside the Oracle server that it must access. The Server-Side Internal driver type is kprb and it actually runs within a default session. You are already "connected". Therefore the connection should 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();

上面是  官方描述 很權威啊 下面是自己的總結

/**

* 測試oracle 的三種連接方式

* 三種連接方式在使用上 沒有太多的不一樣 只是每個方式的使用定位 實現方式不一樣

* thin--多使用在web開發 通過tcp/ip的socket與服務器oracle的tcp/ip的socket監聽器進行連接 進而與oracle服務器進行通信 屬於第4類驅動 獨立於平台的

* oci--不太適合使用在web端 ,因為oracle call interface 通過jni調用本地oracle客戶端的c庫 從而實現與服務器oracle進行通信 屬於第2類驅動

* kprb(server-side internal driver)--為oracle服務器端內部的驅動 用於嵌入在服務器oracle中的java程序/jsp代碼與oracle進行通信 連接使用當前會話用戶名與密碼 這個連接也不用關閉 使用的是kprb的c庫進行通信

* 還有一種 server-side thin ----用於oracle服務器端內部需要訪問遠程服務器時使用或者是同一個數據庫不同的session訪問 例如調用中間件 與client thin沒有太多的區別

* @author Administrator

*

*/

還有一張oracle jdbc的驅動和oracle數據庫的 關系圖

238a870260feb1a752623e79e4295d2b.jpe

好吧 還是來看看具體的例子吧

1、客戶端thin協議 也就是我們常用的jdbc:oracle:thin:@localhost:1521:orcl

thin--多使用在web開發 通過tcp/ip的socket與服務器oracle的tcp/ip的socket監聽器進行連接  進而與oracle服務器進行通信 屬於第4類驅動 獨立於平台的

package com.undergrowth.jdbc.thin;

import java.sql.Connection;

import java.sql.Driver;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Enumeration;

import java.util.Iterator;

import java.util.ServiceLoader;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import oracle.jdbc.OracleDriver;

import oracle.jdbc.pool.OracleDataSource;

/**

* 測試連接oracle的thin協議

* 來源於oracle的ojdbc6的例子

* @author Administrator

*

*/

public class ThinTest {

static final String connect_string = "jdbc:oracle:thin:u1/u1@//localhost:1521/orcl";

static final Logger LOGGER=LoggerFactory.getLogger(ThinTest.class);

// This is the kind of string you would use if going through the

// Oracle 8 connection manager which lets you run the database on a

// different host than the Web Server. See the on-line documentation

// for more information.

// static final String connect_string =

// "jdbc:oracle:thin:hr/hr@(description=(address_list=(address=(protocol=tcp)(host=localhost)(port=1610))(address=(protocol=tcp)(host=localhost)(port=1521)))(source_route=yes)(connect_data=(service_name=orcl.oracle.com)))";

// The query we will execute

static final String query = "select 'Hello JDBC: ' || to_char(sysdate,'yyyy-mm-dd') from dual";

Connection conn;;

StringBuffer output=new StringBuffer();

public String testThin(){

try {

// See if we need to open the connection to the database

if (conn == null) {

// Create a OracleDataSource instance and set URL

OracleDataSource ods = new OracleDataSource();

ods.setURL(connect_string);

// Connect to the databse

output.append("Connecting to " + connect_string + "\n");

conn = ods.getConnection();

output.append("Connected\n");

}

// Create a statement

Statement stmt = conn.createStatement();

// Execute the query

output.append("Executing query " + query + "\n");

ResultSet rset = stmt.executeQuery(query);

// Dump the result

while (rset.next())

output.append(rset.getString(1) + "\n");

// We're done

output.append("done.\n");

LOGGER.info(output.toString());

} catch (Exception e) {

e.printStackTrace();

// Oops

output.append(e.getMessage() + "\n");

LOGGER.info(output.toString());

}finally{

if(conn!=null)

{

try {

conn.close();

conn=null;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return output.toString();

}

/**

* 使用DriverManager進行注冊驅動

* @return

*/

public String testThinDriverManager(){

try {

output=new StringBuffer();

LOGGER.info("開始使用DriverManager進行驅動注冊");

//注冊oracle jdbc驅動

//DriverManager.registerDriver(new OracleDriver());

Enumeration driversEnumeration=DriverManager.getDrivers();

while(driversEnumeration.hasMoreElements()){

System.out.println(driversEnumeration.nextElement().toString());

}

if (conn == null) {

// Create a OracleDataSource instance and set URL

//這里為什么不要 DriverManager.registerDriver(new OracleDriver()) 這么一句話 哈哈 里面有很多學問啊

//簡單的說 就是 DriverManager在類加載的時候 在一個靜態塊中執行了loadInitialDrivers方法 此方法使用

//ServiceLoader.load(Driver.class)的方式加載了位於ojdbc6-11.2.0.jar包下的meta-inf/services/java.sql.Driver文件中的oracle.jdbc.OracleDriver的驅動

//那為什么加載了oracle.jdbc.OracleDriver的驅動 就不需要注冊了呢 原因在於 OracleDriver也有一個靜態塊里面有一個 DriverManager.registerDriver(m_defaultDriver);

//所以無需注冊了

conn=DriverManager.getConnection(connect_string);

// Connect to the databse

output.append("Connecting to " + connect_string + "\n");

output.append("Connected\n");

}

// Create a statement

Statement stmt = conn.createStatement();

// Execute the query

output.append("Executing query " + query + "\n");

ResultSet rset = stmt.executeQuery(query);

// Dump the result

while (rset.next())

output.append(rset.getString(1) + "\n");

// We're done

output.append("done.\n");

LOGGER.info(output.toString());

} catch (Exception e) {

e.printStackTrace();

// Oops

output.append(e.getMessage() + "\n");

LOGGER.info(output.toString());

}finally{

if(conn!=null)

{

try {

conn.close();

conn=null;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return output.toString();

}

}

上面有一段 個人覺得大家可以研究下 還是很有作用的

if (conn == null) {

// Create a OracleDataSource instance and set URL

//這里為什么不要 DriverManager.registerDriver(new OracleDriver()) 這么一句話 哈哈 里面有很多學問啊

//簡單的說 就是 DriverManager在類加載的時候 在一個靜態塊中執行了loadInitialDrivers方法 此方法使用

//ServiceLoader.load(Driver.class)的方式加載了位於ojdbc6-11.2.0.jar包下的meta-inf/services/java.sql.Driver文件中的oracle.jdbc.OracleDriver的驅動

//那為什么加載了oracle.jdbc.OracleDriver的驅動 就不需要注冊了呢 原因在於 OracleDriver也有一個靜態塊里面有一個 DriverManager.registerDriver(m_defaultDriver);

//所以無需注冊了

conn=DriverManager.getConnection(connect_string);

// Connect to the databse

output.append("Connecting to " + connect_string + "\n");

output.append("Connected\n");

}

看到上面的代碼片段 我沒有使用DriverManager.register或者是Class.forName進行驅動的注冊  但是卻依舊可以連接上oracle數據庫  其實看看源碼 和我上面的注釋 就知道原因了 哈哈

對於上面的  補充一下

If you are using JSE 6 and later, then there is no need to explicitly load the JDBC driver. This means that the Java run-time loads the driver when needed and you need not include Class.forName("oracle.jdbc.OracleDriver") or new oracle.jdbc.OracleDriver() in your code. But if you are using J2SE 5.0, then you need to load the JDBC driver explicitly.

2、oci協議

oci--不太適合使用在web端 ,因為oracle call interface 通過jni調用本地oracle客戶端的c庫 從而實現與服務器oracle進行通信 屬於第2類驅動

//常規的寫法

/*String url = "jdbc:oracle:oci:@localhost:1521:orcl";*/

//如果客戶端和服務器在同一台電腦上 使用ipc 進行通信 如下

/* String url ="jdbc:oracle:oci:@";*/

//使用oracle\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora 中的連接項進行連接

String url ="jdbc:oracle:oci8:@ORCL";

看到上面  oci協議有3中寫法 分別對應與不同的情況

第一種   常用方法  jdbc:oracle:oci:@localhost:1521:orcl

第二種 僅適用於 oracle客戶端和oracle服務器在同一台電腦上 使用內部進程間進行通信

第三種 使用tns的配置連接

package com.undergrowth.jdbc.oci;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import java.util.Properties;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import oracle.jdbc.oci.OracleOCIConnection;

import oracle.jdbc.pool.OracleOCIConnectionPool;

/**

* 測試oracle的oci協議

* 來源oracle的ojdbc例子

* @author Administrator

*

*/

public class OciTest {

final static Logger LOGGER=LoggerFactory.getLogger(OciTest.class);

public boolean testOci(){

boolean b=true;

OracleOCIConnectionPool cpool=null;

OracleOCIConnection conn1=null,conn2=null;

ResultSet rset =null;

Statement stmt =null;

//常規的寫法

/*String url = "jdbc:oracle:oci:@localhost:1521:orcl";*/

//如果客戶端和服務器在同一台電腦上 使用ipc 進行通信 如下

/* String url ="jdbc:oracle:oci:@";*/

//使用oracle\Administrator\product\11.2.0\dbhome_1\NETWORK\ADMIN\tnsnames.ora 中的連接項進行連接

String url ="jdbc:oracle:oci8:@ORCL";

try {

String url1 = System.getProperty("JDBC_URL");

if (url1 != null)

url = url1;

// Create an OracleOCIConnectionPool instance with

// default configuration

cpool = new OracleOCIConnectionPool

("u1", "u1", url, null);

// Print out the default configuration for the

// OracleOCIConnectionPool

LOGGER.info

("-- The default configuration for the OracleOCIConnectionPool --");

displayPoolConfig(cpool);

// Get a connection from the pool

conn1 = (OracleOCIConnection)

cpool.getConnection("u1", "u1");

// Create a Statement

stmt = conn1.createStatement ();

// Select the ID, NAME, birthday, age column from the student table

rset = stmt.executeQuery ("SELECT ID, NAME, birthday, age FROM student st WHERE st.id<10");

// Iterate through the result and print the student table

LOGGER.info

("-- Use the connection from the OracleOCIConnectionPool --");

while (rset.next ())

LOGGER.info (rset.getString (1) + " " + rset.getString (2));

LOGGER.info

("-- Use another connection from the OracleOCIConnectionPool --");

// Get another connection from the pool

// with different userID and password

conn2 = (OracleOCIConnection)

cpool.getConnection("u2", "u2");

// Create a Statement

stmt = conn2.createStatement ();

// Select the USER from DUAL to test the connection

rset = stmt.executeQuery ("select to_char(sysdate,'yyyy-mm-dd') from DUAL");

// Iterate through the result and print it out

rset.next ();

LOGGER.info (rset.getString (1));

// Reconfigure the OracleOCIConnectionPool in case the performance

// is too bad. This might happen when many users are trying to connect

// at the same time. In this case, increase MAX_LIMIT to some larger

// number, and also increase INCREMENT to a positive number.

Properties p = new Properties();

p.put (OracleOCIConnectionPool.CONNPOOL_MIN_LIMIT,

Integer.toString(cpool.getMinLimit()));

p.put (OracleOCIConnectionPool.CONNPOOL_MAX_LIMIT,

Integer.toString(cpool.getMaxLimit() * 2)) ;

if (cpool.getConnectionIncrement() > 0)

// Keep the old value

p.put (OracleOCIConnectionPool.CONNPOOL_INCREMENT,

Integer.toString(cpool.getConnectionIncrement()));

else

// Set it to a number larger than 0

p.put (OracleOCIConnectionPool.CONNPOOL_INCREMENT, "1") ;

// Enable the new configuration

cpool.setPoolConfig(p);

// Print out the current configuration for the

// OracleOCIConnectionPool

LOGGER.info

("-- The new configuration for the OracleOCIConnectionPool --");

displayPoolConfig(cpool);

} catch (Exception e) {

// If there is any security exception, ignore it

// and use the default

b=false;

}finally{

// Close the RseultSet

try {

rset.close();

rset = null;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// Close the Statement

try {

stmt.close();

stmt = null;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// Close the connections

try {

conn1.close();

conn1 = null;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

try {

conn2.close();

conn2 = null;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// Close the OracleOCIConnectionPool

try {

cpool.close();

cpool = null;

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return b;

}

// Display the current status of the OracleOCIConnectionPool

private void displayPoolConfig (OracleOCIConnectionPool cpool)

throws SQLException

{

LOGGER.info (" Min poolsize Limit: " + cpool.getMinLimit());

LOGGER.info (" Max poolsize Limit: " + cpool.getMaxLimit());

LOGGER.info (" Connection Increment: " +

cpool.getConnectionIncrement());

LOGGER.info (" NoWait: " + cpool.getNoWait());

LOGGER.info (" Timeout: " + cpool.getTimeout());

LOGGER.info (" PoolSize: " + cpool.getPoolSize());

LOGGER.info (" ActiveSize: " + cpool.getActiveSize());

}

}

3、kprb協議

kprb(server-side internal driver)--為oracle服務器端內部的驅動 用於嵌入在服務器oracle中的java程序/jsp代碼與oracle進行通信 連接使用當前會話用戶名與密碼 這個連接也不用關閉 使用的是kprb的c庫進行通信

package com.undergrowth.jdbc.kprb;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

/**

* 測試oracle的kprb協議

* 來源oracle的安裝文件 oracle\Administrator\product\11.2.0\dbhome_1\javavm\demo\examples\jsproc\basic\basic_kprb

* @author Administrator

* 測試表

*

* create table employee (enum int, ename varchar2(20),

position varchar2(20), salary int);

insert into employee values (100, 'John Doe', 'Secretary', 40000);

insert into employee values (101, 'Jane Johnson', 'Engineer', 60000);

*/

public class KprbTest {

//final static Logger LOGGER=LoggerFactory.getLogger(KprbTest.class);

/*

* update the position and salary of an employee with the

* given employee number

*

* Here, emp_num and raise are IN parameters; position is an IN/OUT

* parameter; salary is an OUT parameter.

*

*/

public static void updatePositionSalary(int emp_num, String[] position,

int[] salary, int raise)

{

String pos = null;

int sal = 0;

Connection conn = null;

Statement stmt = null;

PreparedStatement pstmt = null;

try

{

// create connection and statement

DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver ());

conn = DriverManager.getConnection("jdbc:oracle:kprb:");

stmt = conn.createStatement();

pstmt = conn.prepareStatement("UPDATE EMPLOYEE SET POSITION = ?, " +

" SALARY = SALARY + ? WHERE ENUM = ?");

// set up bind values and execute the update

pstmt.setString(1, position[0]);

pstmt.setInt(2, raise);

pstmt.setInt(3, emp_num);

pstmt.execute();

// retrieve the updated position and salary to verify that

// the data has been updated in the database

ResultSet rset = stmt.executeQuery(

"SELECT POSITION, SALARY FROM EMPLOYEE WHERE ENUM = " + emp_num);

while (rset.next())

{

pos = rset.getString ("position");

sal = rset.getInt ("salary");

}

}catch(SQLException sqlException){

//LOGGER.error(sqlException.getMessage());

System.out.println(sqlException.getMessage());

}finally

{

try {

if(stmt!=null) stmt.close();

} catch (SQLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

position[0] = pos;

salary[0] = sal;

//LOGGER.info("ENUM:"+emp_num+"\t position:"+pos+"\tsalary:"+sal);

System.out.println("ENUM:"+emp_num+"\t position:"+pos+"\tsalary:"+sal);

}

}

}

基於kprb的測試方式 有點特殊 由於其用途所決定的

如下‘

/*

* kprb的測試方法 有點特殊 由於其用途所決定

* 1、編寫好KprbTest.java 編譯成KprbTest.class文件

* 2、將編譯好的KprbTest.class加載到oracle服務器中 作為oracle的模式對象之一

* loadjava -r -f -o -user u1/u1@orcl KprbTest.class

* 查看是否加載成功 SELECT uo.* from User_Objects uo WHERE uo.OBJECT_NAME LIKE '%KprbTest%';

* 3、使用存儲過程或者函數進行包裝你的 KprbTest.class

* create or replace procedure annualReview (enum IN number,

positon IN OUT varchar2, sal OUT number, raise IN number) as

language java name 'com.undergrowth.jdbc.kprb.KprbTest.

updatePositionSalary(int, java.lang.String[], int[], int)';

/

4、test annualReview存儲過程 即可看到結果

begin

-- Call the procedure

annualreview(enum => :enum,

positon => :positon,

sal => :sal,

raise => :raise);

end;

*/

4、附測試類

package com.undergrowth;

import java.sql.SQLException;

import java.util.ServiceLoader;

import com.undergrowth.jdbc.kprb.KprbTest;

import com.undergrowth.jdbc.message.IMessage;

import com.undergrowth.jdbc.oci.OciTest;

import com.undergrowth.jdbc.thin.ThinTest;

import junit.framework.TestCase;

/**

* 測試oracle 的三種連接方式

* 三種連接方式在使用上 沒有太多的不一樣 只是每個方式的使用定位 實現方式不一樣

* thin--多使用在web開發 通過tcp/ip的socket與服務器oracle的tcp/ip的socket監聽器進行連接 進而與oracle服務器進行通信 屬於第4類驅動 獨立於平台的

* oci--不太適合使用在web端 ,因為oracle call interface 通過jni調用本地oracle客戶端的c庫 從而實現與服務器oracle進行通信 屬於第2類驅動

* kprb(server-side internal driver)--為oracle服務器端內部的驅動 用於嵌入在服務器oracle中的java程序/jsp代碼與oracle進行通信 連接使用當前會話用戶名與密碼 這個連接也不用關閉 使用的是kprb的c庫進行通信

* 還有一種 server-side thin ----用於oracle服務器端內部需要訪問遠程服務器時使用或者是同一個數據庫不同的session訪問 例如調用中間件 與client thin沒有太多的區別

* @author Administrator

*

*/

public class OracleJdbcTest extends TestCase {

public void testTestThin() {

ThinTest tt=new ThinTest();

String actual=tt.testThin();

assertNotSame("", actual);

actual=tt.testThinDriverManager();

assertNotSame("", actual);

}

public void testTestOci() {

OciTest tt=new OciTest();

Boolean condition=tt.testOci();

assertTrue(condition);

}

public void testKprbTest() {

/*

* kprb的測試方法 有點特殊 由於其用途所決定

* 1、編寫好KprbTest.java 編譯成KprbTest.class文件

* 2、將編譯好的KprbTest.class加載到oracle服務器中 作為oracle的模式對象之一

* loadjava -r -f -o -user u1/u1@orcl KprbTest.class

* 查看是否加載成功 SELECT uo.* from User_Objects uo WHERE uo.OBJECT_NAME LIKE '%KprbTest%';

* 3、使用存儲過程或者函數進行包裝你的 KprbTest.class

* create or replace procedure annualReview (enum IN number,

positon IN OUT varchar2, sal OUT number, raise IN number) as

language java name 'com.undergrowth.jdbc.kprb.KprbTest.

updatePositionSalary(int, java.lang.String[], int[], int)';

/

4、test annualReview存儲過程 即可看到結果

begin

-- Call the procedure

annualreview(enum => :enum,

positon => :positon,

sal => :sal,

raise => :raise);

end;

*/

/*KprbTest tt=new KprbTest();

int[] salary=new int[1];

String[] position=new String[1];

tt.updatePositionSalary(100, position, salary, 60000);

assertNotSame("", position[0]);*/

}

/**

* 測試服務加載者用於加載位於meta-inf/services/下服務提供者

*/

public void testServiceLoader(){

ServiceLoader messageLoader=ServiceLoader.load(IMessage.class);

for (IMessage iMessage : messageLoader) {

System.out.println(iMessage.getBuffer().toString());

}

}

}

pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.undergrowth

jdbc

0.0.1-SNAPSHOT

jar

jdbc

http://maven.apache.org

UTF-8

junit

junit

3.8.1

test

com.oracle

ojdbc6

11.2.0

org.slf4j

slf4j-log4j12

1.7.7

哦 對了 對於oracle的maven依賴 我們需要自己安裝到版本倉庫中

參考如下

mvn install:install-file -Dfile={Path/to/your/ojdbc.jar} -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0 -Dpackaging=jar詳細參考

http://www.cnblogs.com/leiOOlei/archive/2013/10/21/3380568.html

5、最后來看看 注冊驅動類DriverManager 是如何與oracle的OriaclDriver掛上關系的

恩 這里為了不是事情更復雜化 選擇常用方法  DriverManager.registerDriver方法入手

DriverManager.registerDriver(new OracleDriver());

看看registerDriver方法

/**

* Registers the given driver with the DriverManager.

* A newly-loaded driver class should call

* the method registerDriver to make itself

* known to the DriverManager.

*

* @param driver the new JDBC Driver that is to be registered with the

* DriverManager

* @exception SQLException if a database access error occurs

*/

public static synchronized void registerDriver(java.sql.Driver driver)

throws SQLException {

/* Register the driver if it has not already been added to our list */

if(driver != null) {

registeredDrivers.addIfAbsent(new DriverInfo(driver));

} else {

// This is for compatibility with the original DriverManager

throw new NullPointerException();

}

println("registerDriver: " + driver);

}

synchronized字節碼進行同步鎖  查看addIfAbsent

添加一個不存在的元素到列表中

/**

* Append the element if not present.

*

* @param e element to be added to this list, if absent

* @return true if the element was added

*/

public boolean addIfAbsent(E e) {

final ReentrantLock lock = this.lock;

lock.lock();

try {

// Copy while checking if already present.

// This wins in the most common case where it is not present

Object[] elements = getArray();

int len = elements.length;

Object[] newElements = new Object[len + 1];

for (int i = 0; i < len; ++i) {

if (eq(e, elements[i]))

return false; // exit, throwing away copy

else

newElements[i] = elements[i];

}

newElements[len] = e;

setArray(newElements);

return true;

} finally {

lock.unlock();

}

}

使用 ReentrantLock保證了同步

重點是元素是否相等    if (eq(e, elements[i]))

/**

* Test for equality, coping with nulls.

*/

private static boolean eq(Object o1, Object o2) {

return (o1 == null ? o2 == null : o1.equals(o2));

}

當注冊驅動時 o1==null 為false  執行  o1.equals(o2)

/**

* Compares the specified object with this list for equality.

* Returns {@code true} if the specified object is the same object

* as this object, or if it is also a {@link List} and the sequence

* of elements returned by an {@linkplain List#iterator() iterator}

* over the specified list is the same as the sequence returned by

* an iterator over this list. The two sequences are considered to

* be the same if they have the same length and corresponding

* elements at the same position in the sequence are equal.

* Two elements {@code e1} and {@code e2} are considered

* equal if {@code (e1==null ? e2==null : e1.equals(e2))}.

*

* @param o the object to be compared for equality with this list

* @return {@code true} if the specified object is equal to this list

*/

public boolean equals(Object o) {

if (o == this)

return true;

if (!(o instanceof List))

return false;

List> list = (List>)(o);

Iterator> it = list.iterator();

Object[] elements = getArray();

int len = elements.length;

for (int i = 0; i < len; ++i)

if (!it.hasNext() || !eq(elements[i], it.next()))

return false;

if (it.hasNext())

return false;

return true;

}

當向

// List of registered JDBC drivers

private final static CopyOnWriteArrayList registeredDrivers = new CopyOnWriteArrayList();

添加了驅動后  即 DriverManager.registerDriver(new OracleDriver());  執行完了

接着第二步是

conn=DriverManager.getConnection(connect_string);

/**

* Attempts to establish a connection to the given database URL.

* The DriverManager attempts to select an appropriate driver from

* the set of registered JDBC drivers.

*

* @param url a database url of the form

* jdbc:subprotocol:subname

* @return a connection to the URL

* @exception SQLException if a database access error occurs

*/

public static Connection getConnection(String url)

throws SQLException {

java.util.Properties info = new java.util.Properties();

// Gets the classloader of the code that called this method, may

// be null.

ClassLoader callerCL = DriverManager.getCallerClassLoader();

return (getConnection(url, info, callerCL));

}

接着看 getConnection方法

// Worker method called by the public getConnection() methods.

private static Connection getConnection(

String url, java.util.Properties info, ClassLoader callerCL) throws SQLException {

/*

* When callerCl is null, we should check the application's

* (which is invoking this class indirectly)

* classloader, so that the JDBC driver class outside rt.jar

* can be loaded from here.

*/

synchronized(DriverManager.class) {

// synchronize loading of the correct classloader.

if(callerCL == null) {

callerCL = Thread.currentThread().getContextClassLoader();

}

}

if(url == null) {

throw new SQLException("The url cannot be null", "08001");

}

println("DriverManager.getConnection(\"" + url + "\")");

// Walk through the loaded registeredDrivers attempting to make a connection.

// Remember the first exception that gets raised so we can reraise it.

SQLException reason = null;

for(DriverInfo aDriver : registeredDrivers) {

// If the caller does not have permission to load the driver then

// skip it.

if(isDriverAllowed(aDriver.driver, callerCL)) {

try {

println(" trying " + aDriver.driver.getClass().getName());

Connection con = aDriver.driver.connect(url, info);

if (con != null) {

// Success!

println("getConnection returning " + aDriver.driver.getClass().getName());

return (con);

}

} catch (SQLException ex) {

if (reason == null) {

reason = ex;

}

}

} else {

println(" skipping: " + aDriver.getClass().getName());

}

}

// if we got here nobody could connect.

if (reason != null) {

println("getConnection failed: " + reason);

throw reason;

}

println("getConnection: no suitable driver found for "+ url);

throw new SQLException("No suitable driver found for "+ url, "08001");

}

重點來了

Connection con = aDriver.driver.connect(url, info);

這里的  aDriver.driver  就是我們上面注冊的 OracleDriver了  那看看OracleDriver的connect是什么吧

public Connection connect(String s, Properties properties)

throws SQLException

{

if(s.regionMatches(0, "jdbc:default:connection", 0, 23))

{

String s1 = "jdbc:oracle:kprb";

int j = s.length();

if(j > 23)

s = s1.concat(s.substring(23, s.length()));

else

s = s1.concat(":");

s1 = null;

}

int i = oracleAcceptsURL(s);

if(i == 1)

return null;

if(i == 2)

{

DBError.throwSqlException(67);

return null;

}

Hashtable hashtable = parseUrl(s);

if(hashtable == null)

return null;

String s2 = properties.getProperty("user");

String s3 = properties.getProperty("password");

String s4 = properties.getProperty("database");

if(s4 == null)

s4 = properties.getProperty("server");

if(s2 == null)

s2 = (String)hashtable.get("user");

s2 = parseLoginOption(s2, properties);

if(s3 == null)

s3 = (String)hashtable.get("password");

if(s4 == null)

s4 = (String)hashtable.get("database");

String s5 = (String)hashtable.get("protocol");

properties.put("protocol", s5);

if(s5 == null)

{

DBError.throwSqlException(40, "Protocol is not specified in URL");

return null;

}

String s6 = properties.getProperty("dll");

if(s6 == null)

properties.put("dll", "ocijdbc9");

String s7 = properties.getProperty("prefetch");

if(s7 == null)

s7 = properties.getProperty("rowPrefetch");

if(s7 == null)

s7 = properties.getProperty("defaultRowPrefetch");

if(s7 != null && Integer.parseInt(s7) <= 0)

s7 = null;

String s8 = properties.getProperty("batch");

if(s8 == null)

s8 = properties.getProperty("executeBatch");

if(s8 == null)

s8 = properties.getProperty("defaultExecuteBatch");

if(s8 != null && Integer.parseInt(s8) <= 0)

s8 = null;

String s9 = properties.getProperty("remarks");

if(s9 == null)

s9 = properties.getProperty("remarksReporting");

String s10 = properties.getProperty("synonyms");

if(s10 == null)

s10 = properties.getProperty("includeSynonyms");

String s11 = properties.getProperty("restrictGetTables");

String s12 = properties.getProperty("fixedString");

String s13 = properties.getProperty("dataSizeUnits");

String s14 = properties.getProperty("AccumulateBatchResult");

if(s14 == null)

s14 = "true";

Enumeration enumeration;

for(enumeration = DriverManager.getDrivers(); enumeration.hasMoreElements();)

{

Driver driver = (Driver)enumeration.nextElement();

if(driver instanceof OracleDriver)

break;

}

while(enumeration.hasMoreElements())

{

Driver driver1 = (Driver)enumeration.nextElement();

if(driver1 instanceof OracleDriver)

DriverManager.deregisterDriver(driver1);

}

Connection connection = getConnectionInstance(s5, s, s2, s3, s4, properties);

if(s7 != null)

((oracle.jdbc.driver.OracleConnection)connection).setDefaultRowPrefetch(Integer.parseInt(s7));

if(s8 != null)

((oracle.jdbc.driver.OracleConnection)connection).setDefaultExecuteBatch(Integer.parseInt(s8));

if(s9 != null)

((oracle.jdbc.driver.OracleConnection)connection).setRemarksReporting(s9.equalsIgnoreCase("true"));

if(s10 != null)

((oracle.jdbc.driver.OracleConnection)connection).setIncludeSynonyms(s10.equalsIgnoreCase("true"));

if(s11 != null)

((oracle.jdbc.driver.OracleConnection)connection).setRestrictGetTables(s11.equalsIgnoreCase("true"));

if(s12 != null)

((oracle.jdbc.driver.OracleConnection)connection).setDefaultFixedString(s12.equalsIgnoreCase("true"));

if(s13 != null)

((oracle.jdbc.driver.OracleConnection)connection).setDataSizeUnits(s13);

((oracle.jdbc.driver.OracleConnection)connection).setAccumulateBatchResult(s14.equalsIgnoreCase("true"));

hashtable = null;

return connection;

}

看到 好多 啊  其實 顯示解析URL和解析默認屬性  中間才是連接 后面又是解析URL和設置默認屬性

主要的是 這個方法

注意哦 這個地方s5  如果我們用的是thin的協議的話 s5=thin 了  這個很有用啊  直接關系到下面找不找的具體的socket代碼

Connection connection = getConnectionInstance(s5, s, s2, s3, s4, properties);

看到下面的連接方法

private Connection getConnectionInstance(String s, String s1, String s2, String s3, String s4, Properties properties)

throws SQLException

{

Object obj = null;

if(s.compareTo("ultra") == 0)

{

try

{

Class aclass[] = null;

Object aobj[] = new Object[6];

aobj[0] = s;

aobj[1] = s1;

aobj[2] = s2;

aobj[3] = s3;

aobj[4] = s4;

aobj[5] = properties;

Class class1 = Class.forName("oracle.jdbc.ultra.client.Driver");

Method amethod[] = class1.getMethods();

for(int i = 0; i < amethod.length; i++)

{

if(!amethod[i].getName().equals("getConnection"))

continue;

aclass = amethod[i].getParameterTypes();

break;

}

Method method = class1.getMethod("getConnection", aclass);

obj = (Connection)method.invoke(class1.newInstance(), aobj);

}

catch(Exception exception)

{

exception.printStackTrace();

DBError.throwSqlException(1);

}

} else

{

String s5 = null;

if(s.equals("thin") && System.getProperty("oracle.jserver.version") != null)

s5 = "thin-server";

else

if((s.equals("oci8") || s.equals("oci")) && System.getProperty("oracle.jserver.version") != null)

s5 = "oci-server";

else

s5 = s;

String s6 = (String)m_driverAccess.get(s5);

if(s6 == null)

DBError.throwSqlException(67, "Invalid protocol " + s);

DBAccess dbaccess = null;

try

{

dbaccess = (DBAccess)Class.forName(s6).newInstance();

}

catch(Exception _ex)

{

return null;

}

if(properties.getProperty("is_connection_pooling") == "true")

{

properties.put("database", s4 != null ? ((Object) (s4)) : "");

obj = new OracleOCIConnection(dbaccess, s1, s2, s3, s4, properties);

} else

{

obj = new oracle.jdbc.driver.OracleConnection(dbaccess, s1, s2, s3, s4, properties);

}

}

return ((Connection) (obj));

}

有兩句話比較關鍵

String s5 = null;

if(s.equals("thin") && System.getProperty("oracle.jserver.version") != null)

s5 = "thin-server";

else

if((s.equals("oci8") || s.equals("oci")) && System.getProperty("oracle.jserver.version") != null)

s5 = "oci-server";

else

s5 = s;

String s6 = (String)m_driverAccess.get(s5);哈哈  看到上面說了  如果使用的是 client-thin的話  那么s5=s=thin

那么s6的值是什么呢

m_driverAccess = new Properties();

m_driverAccess.put("thin-server", "oracle.jdbc.thinserver.ServerTTC7Protocol");

m_driverAccess.put("oci-server", "oracle.jdbc.ociserver.ServerOCIDBAccess");

m_driverAccess.put("thin", "oracle.jdbc.ttc7.TTC7Protocol");

m_driverAccess.put("oci8", "oracle.jdbc.oci8.OCIDBAccess");

m_driverAccess.put("oci", "oracle.jdbc.oci8.OCIDBAccess");

m_driverAccess.put("kprb", "oracle.jdbc.kprb.KprbDBAccess");

m_defaultDriver = null;這個實在OracleDriver的靜態代碼塊中的

所以s6=  oracle.jdbc.ttc7.TTC7Protocol    就是你了

我看了看  當使用的是thin寫的時候

執行 最后一步

obj = new oracle.jdbc.driver.OracleConnection(dbaccess, s1, s2, s3, s4, properties);

所以此時 傳遞的dbaccess及為 TTC7Protocal的實例了

dbaccess = (DBAccess)Class.forName(s6).newInstance();

好的 知道了 這里 DBAccess實際上是指向TTC7Protocal的實例  就可以了

接着走

oracle.jdbc.driver.OracleDriver的構造函數

public OracleConnection(DBAccess dbaccess, String s, String s1, String s2, String s3, Properties properties)

throws SQLException

{

m_txn_mode = 0;

m_clientIdSet = false;

m_clientId = null;

include_synonyms = false;

restrict_getTables = false;

m_accumulateBatchResult = true;

dataSizeScale = 1;

m_dbMetaData = null;

m_opc = null;

m_opc_oc = null;

m_osql = null;

m_warning = null;

m_readOnly = false;

m_startTime = 0L;

statementCache = null;

m_stmtClearMetaData = false;

m_process_escapes = true;

m_defaultAutoRefetch = true;

m_occ = null;

m_privData = null;

defaultFixedString = false;

m_svptStmt = null;

wrapper = null;

connectionProperties = null;

m_sessionTimeZone = null;

m_dbTzCalendar = null;

String s4 = null;

Object obj = null;

boolean flag = true;

if(properties != null)

{

s4 = (String)properties.get("protocol");

String s6 = properties.getProperty("processEscapes");

if(s6 != null && s6.equalsIgnoreCase("false"))

m_process_escapes = false;

connectionProperties = (Properties)properties.clone();

connectionProperties.remove("password");

}

initialize(s, s1, s4, dbaccess, null, null, null, s3);

logicalHandle = false;

try

{

needLine();

conversion = db_access.logon(s1, s2, s3, properties);

m_warning = DBError.addSqlWarning(m_warning, db_access.getWarnings());

if(properties == null || properties.getProperty("connection_pool") != "connection_pool")

{

default_row_prefetch = db_access.getDefaultPrefetch();

if(properties != null)

{

String s5 = properties.getProperty("autoCommit");

if(s5 != null && s5.equalsIgnoreCase("false"))

flag = false;

}

setAutoCommit(flag);

db_access.initNls(this);

}

}

catch(IOException ioexception)

{

DBError.throwSqlException(ioexception);

}

catch(SQLException sqlexception)

{

try

{

db_access.logoff();

}

catch(IOException _ex) { }

catch(SQLException _ex) { }

throw sqlexception;

}

m_txn_mode = 0;

}

其實只有一句最關鍵

conversion = db_access.logon(s1, s2, s3, properties);

這里就是用的TTC7Protocal的實例啊    的logon方法

接着

public synchronized DBConversion logon(String s, String s1, String s2, Properties properties)

throws SQLException, IOException

{

try

{

if(state > 0)

DBError.check_error(428);

if(s == null || s1 == null)

DBError.check_error(433);

if(s.length() == 0 || s1.length() == 0)

DBError.check_error(443);

if(s2 == null)

s2 = "localhost:1521:orcl";

connect(s2, properties);

all7 = new Oall7(MEngine);

commoncall = new Ocommoncall(MEngine);

opencall = new Oopen(MEngine);

close = new Oclose(MEngine);

TTCTypeRep _tmp = MEngine.types;

describe = (Odscrarr)MEngine.types.newTTIFunObject((byte)1, MEngine);

bfileMsg = new v8TTIBfile(MEngine);

blobMsg = new v8TTIBlob(MEngine);

clobMsg = new v8TTIClob(MEngine);

TTCTypeRep _tmp1 = MEngine.types;

dty = (TTIdty)MEngine.types.newTTCMsgObject((byte)2, MEngine);

dty.marshal();

dty.receive();

String s3 = (String)properties.get("internal_logon");

if(s3 != null)

if(s3.equalsIgnoreCase("sysoper"))

LOGON_MODE = 16L;

else

if(s3.equalsIgnoreCase("sysdba"))

LOGON_MODE = 8L;

log1 = new O3log(MEngine, s, properties, LOGON_MODE);

log1.marshal();

log1.receive1st();

log2 = new O3log(MEngine, s, s1, log1.encryptedSK, properties, LOGON_MODE);

log2.marshal();

do

try

{

log2.receive2nd();

break;

}

catch(SQLWarning sqlwarning)

{

setWarnings(DBError.addSqlWarning(getWarnings(), sqlwarning));

}

while(true);

net.setO3logSessionKey(log2.getSessionKey());

ver = new Oversion(MEngine);

ver.marshal();

ver.receive();

short word0 = ver.getVersionNumber();

if(word0 < 7230)

DBError.check_error(441);

MEngine.types.setVersion(word0);

state = 1;

return MEngine.conv;

}

catch(SQLException sqlexception)

{

try

{

net.disconnect();

}

catch(Exception exception) { }

state = 0;

throw sqlexception;

}

}

看到有一句

connect(s2, properties);

private void connect(String s, Properties properties)

throws IOException, SQLException

{

if(s == null || properties == null)

DBError.check_error(433);

net = new NSProtocol();

try

{

net.connect(s, properties);

}

catch(NetException netexception)

{

throw new IOException(netexception.getMessage());

}

MEngine = new MAREngine(net);

pro = new v8TTIpro(MEngine);

pro.marshal();

pro.receive();

short word0 = pro.getOracleVersion();

short word1 = pro.getCharacterSet();

short word2 = TTCConversion.findAccessCharSet(word1, word0);

TTCConversion ttcconversion = new TTCConversion(word1, word2, word0, pro.getncharCHARSET());

MEngine.types.setServerConversion(word2 != word1);

MEngine.types.setVersion(word0);

if(DBConversion.isCharSetMultibyte(word2))

{

if(DBConversion.isCharSetMultibyte(pro.getCharacterSet()))

MEngine.types.setFlags((byte)1);

else

MEngine.types.setFlags((byte)2);

} else

{

MEngine.types.setFlags(pro.getFlags());

}

MEngine.conv = ttcconversion;

}

進行連接的部分

net = new NSProtocol();

try

{

net.connect(s, properties);

}

嘎嘎 接着找

NSProtocal的connect

public void connect(String s, Properties properties)

throws IOException, NetException

{

if(sAtts.connected)

throw new NetException(201);

if(s == null)

throw new NetException(208);

addrRes = new AddrResolution(s, properties);

if(addrRes.connection_revised)

{

s = addrRes.getTNSAddress();

properties = addrRes.getUp();

}

if(addrRes.jndi)

sAtts.profile = new ClientProfile(properties, addrRes.getJndi());

else

sAtts.profile = new ClientProfile(properties);

establishConnection(s);

Object obj4 = null;

try

{

obj4 = Class.forName("oracle.net.ano.Ano").newInstance();

sAtts.anoEnabled = true;

}

catch(Exception _ex)

{

sAtts.anoEnabled = false;

}

if(obj4 != null)

{

((Ano)obj4).init(sAtts);

sAtts.ano = (Ano)obj4;

sAtts.anoEnabled = true;

}

label0:

do

{

ConnectPacket connectpacket = new ConnectPacket(sAtts);

connectpacket.send();

packet = new Packet(sAtts);

packet.receive();

switch(packet.type)

{

case 2: // '\002'

AcceptPacket acceptpacket = new AcceptPacket(packet);

break label0;

case 11: // '\013'

break;

case 5: // '\005'

RedirectPacket redirectpacket = new RedirectPacket(packet);

sAtts.cOption.nt.disconnect();

sAtts = establishConnection(redirectpacket.getData());

break;

case 4: // '\004'

RefusePacket refusepacket = new RefusePacket(packet);

throw new NetException(206, refusepacket.getData());

case 3: // '\003'

case 6: // '\006'

case 7: // '\007'

case 8: // '\b'

case 9: // '\t'

case 10: // '\n'

default:

sAtts.cOption.nt.disconnect();

throw new NetException(205);

}

} while(true);

setNetStreams();

sAtts.connected = true;

if(sAtts.ano != null)

sAtts.ano.negotiation();

packet = null;

Object obj = null;

Object obj1 = null;

Object obj3 = null;

Object obj2 = null;

}

關鍵的語句

establishConnection(s);

private SessionAtts establishConnection(String s)

throws NetException, IOException

{

sAtts.cOption = addrRes.resolveAndExecute(s);

sAtts.ntInputStream = sAtts.cOption.nt.getInputStream();

sAtts.ntOutputStream = sAtts.cOption.nt.getOutputStream();

sAtts.setTDU(sAtts.cOption.tdu);

sAtts.setSDU(sAtts.cOption.sdu);

sAtts.nsOutputStream = new NetOutputStream(sAtts, 255);

sAtts.nsInputStream = new NetInputStream(sAtts);

return sAtts;

}哈哈  看到這 可能就迷糊了 啊   接下去怎么看呢  嘎嘎  關鍵在第一句

sAtts.cOption = addrRes.resolveAndExecute(s);

public ConnOption resolveAndExecute(String s) throws NetException, IOException { cs = new ConnStrategy(); if(s.indexOf("//") != -1) resolveUrl(s); else if(s.indexOf(':') != -1 && s.indexOf(')') == -1) resolveSimple(s); else if(newSyntax) resolveAddrTree(s); else resolveAddr(s); if(!cs.optAvailable()) return cs.execute(); else return cs.getOption(); }

返回一個ConnOptions  是干什么的呢  看看就知道了

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.

// Jad home page: http://www.kpdus.com/jad.html

// Decompiler options: packimports(3)

package oracle.net.nt;

import java.io.IOException;

import oracle.net.nl.NLException;

import oracle.net.ns.NetException;

// Referenced classes of package oracle.net.nt:

// NTAdapter, TcpNTAdapter

public class ConnOption

{

public ConnOption()

{

conn_data = new StringBuffer();

}

public void connect()

throws IOException

{

nt = getNT();

nt.connect();

}

private NTAdapter getNT()

throws NetException

{

try

{

nt = new TcpNTAdapter(addr);

}

catch(NLException _ex)

{

throw new NetException(501);

}

return nt;

}

public NTAdapter nt;

public int port;

public int tdu;

public int sdu;

public String protocol;

public String host;

public String sid;

public String addr;

public String service_name;

public String instance_name;

public StringBuffer conn_data;

public boolean done;

}

看到了吧 ConnOptions封裝了一個TcpNTAdapter 嘎嘎  看看是干什么的

// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.

// Jad home page: http://www.kpdus.com/jad.html

// Decompiler options: packimports(3)

package oracle.net.nt;

import java.io.*;

import java.net.Socket;

import oracle.net.nl.*;

// Referenced classes of package oracle.net.nt:

// NTAdapter

public class TcpNTAdapter

implements NTAdapter

{

public TcpNTAdapter(String s)

throws NLException

{

NVNavigator nvnavigator = new NVNavigator();

NVPair nvpair = (new NVFactory()).createNVPair(s);

if(nvpair == null)

throw new NLException((short)100);

NVPair nvpair1 = nvnavigator.findNVPair(nvpair, "PROTOCOL");

NVPair nvpair2 = nvnavigator.findNVPair(nvpair, "HOST");

NVPair nvpair3 = nvnavigator.findNVPair(nvpair, "PORT");

if(nvpair1 == null || nvpair2 == null || nvpair3 == null)

throw new NLException((short)100);

prot = nvpair1.getAtom();

host = nvpair2.getAtom();

port = Integer.parseInt(nvpair3.getAtom());

if(!prot.equals("TCP") && !prot.equals("tcp"))

throw new NLException((short)100);

else

return;

}

public void connect()

throws IOException

{

socket = new Socket(host, port);

}

public void disconnect()

throws IOException

{

socket.close();

socket = null;

}

public InputStream getInputStream()

throws IOException

{

return socket.getInputStream();

}

public OutputStream getOutputStream()

throws IOException

{

return socket.getOutputStream();

}

static final boolean DEBUG = false;

int port;

String host;

String prot;

private Socket socket;

}

嘎嘎  看到了吧 TcpNTAdapter有一個connect 方法

public void connect()

throws IOException

{

socket = new Socket(host, port);

}就建立了一個socket進行連接啊

以上即是 oracle的jdbc的連接過程 沒多講  這個領會多少 就看個人悟性了 噶

以上即是oracle的 jdbc的幾種方式 記錄學習的腳步

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Oracle Database 12c Release 1 (12.1.0.1) JDBC Drivers SimpleFAN Download simplefan.jar (21,048 bytes) - Classes for subscribing to RAC events via ONS; simplefan policy and javadoc JDBC Thin for All Platforms Download demo.tar (686,080 bytes) - JDBC Demos (TAR format) Download demo.zip (614,561 bytes) -JDBC Demos (ZIP format) Download JavaDoc (7,103,997 bytes) Download README Download ojdbc6.jar (3,389,454 bytes) - For use with JDK 6; It contains the JDBC driver classes except classes for NLS support in Oracle Object and Collection types. Download ojdbc6_g.jar (5,382,573 bytes) - Same as ojdbc6.jar except compiled with "javac -g" and contains tracing code. Download ojdbc6dms.jar (4,033,913 bytes) - Same as ojdbc6.jar, except that it contains instrumentation to support DMS and limited java.util.logging calls. Download ojdbc6dms_g.jar (5,405,632 bytes) - Same as ojdbc6_g.jar except that it contains instrumentation to support DMS. Download ojdbc7.jar (3,397,734 bytes) - For use with JDK 7; It contains the JDBC driver classes except classes for NLS support in Oracle Object and Collection types. Download ojdbc7_g.jar (5,395,834 bytes) - Same as ojdbc7.jar except compiled with "javac -g" and contains tracing code. Download ojdbc7dms.jar (4,044,963 bytes) - Same as ojdbc7.jar, except that it contains instrumentation to support DMS and limited java.util.logging calls. Download ojdbc7dms_g.jar (5,419,088 bytes) - Same as ojdbc7_g.jar except that it contains instrumentation to support DMS. Download orai18n.jar (1,659,576 bytes) - Classes for NLS support Download xdb6.jar (263,690 bytes) - Classes to support standard JDBC 4.x java.sql.SQLXML interface (Java SE 6 & Java SE 7). 附件里的文件都可以在这里下载,只是不用再注册用户了: http://www.oracle.com/technetwork/database/features/jdbc/jdbc-drivers-12c-download-1958347.html

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值