java與oracle的接口:
在數據庫中運行JAVA可以說是ORACLE8i的最令人激動的新特性。在你創建的使用ORACLE8i 數據庫的應用程序中,你可以使用與JAVA有關的新特征,輕松的將程序發布到INTERNET或INTRANET上。
Methods for Using Java in ORACLE
大家都知道JAVA在跨平台開發與INTERNET開發中已經比較流行,ORACLE8i及以後的版本中都包含了對在數據庫中運行JAVA的擴展支持,這裡有兩種方法可以使用:
JDBC:與ODBC類似, JDBC 提供了一個驅動接口使你可以在JAVA程序中訪問數據庫。注:JDBC驅動內嵌在數據庫中虛擬機中。
SQLJ:是一個JAVA預編譯器,它可以將內嵌的SQL語句轉化為JAVA語句.SQLJ的使用與運行機理與其它ORACLE的與編譯器(如Pro*C,Pro*COBOL)類似。實際上,為了使我們形象的記住SQLJ提供的功能,我們也可以直接將SQLJ改名為Pro*Java。
將JAVA集成到數據庫中是雙向的。也就是說你可以在JAVA中調用SQL與PL/SQL,也可以在SQL與PL/SQL中調用JAVA。JAVA程序可以直接通過JDBC驅動調用SQL與PL/SQL,反過來,你也可以在SQL與PL/SQL中直接調用JAVA。在數據庫中,JAVA命名空間直接映射到數據庫模式的命名空間中,這樣可以方便JAVA的存取與調用。數據庫同時提供擴展的DDL語句,通過這些語句,你可以象創建一個存儲過程一樣在數據中創建內嵌的JAVA程序。
Features of ORACLE JDBC Drivers
在ORACLE8i中有三種類型的JDBC驅動,他們都使用相同的 syntax, APIs, and Oracle extensions,以使JAVA代碼在robust clients、Web-based Java applets, and Java stored procedures之間保持輕便靈活:三種類型如下:
1.JDBC OCI: 此驅動類似於傳統的ODBC 驅動。因為它需要Oracle Call Interface and Net8,所以它需要在運行使用此驅動的JAVA程序的機器上安裝客戶端軟件
2.JDBC Thin: 這種驅動一般用在運行在WEB浏覽器中的JAVA程序。它不是通過OCI or Net8,而是通過Java sockets進行通信 ,因此不需要在使用JDBC Thin的客戶端機器上安裝客戶端軟件。
URL:http://www.bianceng.cn/Programming/Java/201410/45818.htm
3.JDBC KPRB: 這種驅動由直接存儲在數據庫中的JAVA程序使用,如Java Stored Procedures 、triggers、Database JSP's。It uses the default/ current database session and thus requires no additional database username, password or URL.
如何配置使JAVA可以通過Oracle JDBC Drivers連接到數據庫:1.安裝Sun JDK.
2. 修改PATH環境變量,使其指向JDK的bin目錄
3. 設置CLASSPATH環境變量,使其指向正確的JDK的lib及oracle的JDBC接口。
CLASSPATH = ".;????"
3. 運行"java –version" ,驗證java的版本。
如何在不同的操作系統上根據接口類型設置客戶端:
對JDBC THIN接口:
在windows與unix下的設置方法一樣:
1.根據jdk的版本,只需要將classesxx.zip拷貝到指定的目錄,不需要安裝Oracle Client。在裝完數據庫後,該文件會在$ORACLE_HOME/jdbc/lib目錄下。2.設置CLASSPATH,使其包含上面的classesxx.zip
3.根據需要,拷貝oracle的其它zip文件並設置CLASSPATH
對JDBC OCI接口:
Fow Windows:
1.安裝Oracle Client.
2.根據jdk的版本,設置CLASSPATH,使其包含正確的classesxx.zip
3.根據需要設置CLASSPATH,使其指向Oracle的其它zip文件
4.設置PATH,使其包含$ORACLE_HOME\bin目錄
For unix:
1.安裝Oracle Client.
2.根據jdk的版本,設置CLASSPATH,使其包含正確的classesxx.zip
3.根據需要設置CLASSPATH,使其指向Oracle的其它zip文件
4.設置LD_LIBRARY_PATH,使其包含$ORACLE_HOME/lib目錄
備注:
classesxx.zip一般在ORACLE_HOME\jdbc\lib目錄下。
在ORACLE_HOME\jdbc\lib目錄下的與Oracle JDBC Drives驅動有關的文件的解釋:
- classes12.zip
Classes for use with JDK 1.2.x. It contains the JDBC driver
classes except classes necessary for NLS support in Object and
Collection types.
- nls_charset12.zip
NLS classes for use with JDK 1.2.x. It contains classes necessary
for NLS support in Object and Collection types.
- classes12_g.zip
Same as classes12.zip, except that classes were compiled with
"javac -g".
JDBC連接數據庫的語法:
JDBC THIN:
Code: [Copy to clipboard]
Connection conn=
DriverManager.getConnection
("jdbc:oracle:thin:@dlsun511:1521:ora1","scott","tiger");
| | |
machine(ip@) : port# : sid
JDBC OCI:
Code: [Copy to clipboard]
Connection conn=
DriverManager.getConnection
("jdbc:oracle:oci8[9]:@RAC","scott","tiger");
|
Net Service
JDBC THIN與JDBC THIN對比:
相同之處:
The JDBC Thin, JDBC OCI, and JDBC Server drivers all provide the same functionality. They all support the following standards and features:
* JDBC 2.0
* Partial JDBC 3.0 (in JDBC driver version 9.2)
* the same syntax and APIs
* the same Oracle extensions
至於不同之處是一個表格,不好上傳,大家自己總結吧!!
主要是JDBC OCI 接口比JDBC THIN接口效率高!
How does one connect with the JDBC Thin Driver?
The the JDBC thin driver provides the only way to access Oracle from the Web (applets). It is smaller and slower than the OCI drivers.
import java.sql.*;
Code: [Copy to clipboard]
class dbAccess {
public static void main (String args []) throws SQLException
{
DriverManager.registerDriver (
new oracle.jdbc.driver.OracleDriver()
);
Connection conn = DriverManager.getConnection
("jdbc:oracle:thin:@dbhost:1521:ORA1", "scott", "tiger");
// @machine:port:SID, userid, password
//URL:http://www.bianceng.cn/Programming/Java/201410/45818.htm
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 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.OracleDriver");
} 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 OracleDriver.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.OracleDriver()).defaultConnection();
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery (
"select BANNER from SYS.V_$VERSION"
);