Features of ORACLE JDBC Drivers ================================= 在ORACLE8i中有三種類(lèi)型的JDBC驅動(dòng),他們都使用相同的 syntax, APIs, and Oracle extensions,以使JAVA代碼在robust clients、Web-based Java applets, and Java stored procedures之間保持輕便靈活:三種類(lèi)型如下: 1.JDBC OCI:此驅動(dòng)類(lèi)似于傳統的ODBC 驅動(dòng)。因為它需要Oracle Call Interface and Net8,所以它需要在運行使用此驅動(dòng)的JAVA程序的機器上安裝客戶(hù)端軟件 2.JDBC Thin:這種驅動(dòng)一般用在運行在WEB瀏覽器中的JAVA程序。它不是通過(guò)OCI or Net8,而是通過(guò)Java sockets進(jìn)行通信,因此不需要在使用JDBC Thin的客戶(hù)端機器上安裝客戶(hù)端軟件。 3.JDBC KPRB:這種驅動(dòng)由直接存儲在數據庫中的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.
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驅動(dòng)有關(guān)的文件的解釋?zhuān)?span lang="EN-US"> - 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".
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
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() ;
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" ; while (rset.next()) System.out.println (rset.getString(1)); // Print col 1 stmt.close(); } }
與JAVA有關(guān)的初始化參數:====================================== Executing initjvm.sql also highlights some new initsid.ora parameters that are used to support Java in your Oracle8i database. These parameters, their descriptions, and the settings required for running initjvm.sql, are all shown in the following list: SHARED_POOL_SIZE? Defines the size of your shared pool in bytes. This should be set to at least 50MB to run initjvm.sql. JAVA_POOL_SIZE? Defines the size of the Java pool, a new area of the SGA in Oracle8i used to store shared Java objects. This should be set to 50MB when running initjvm.sql, but can be as low as 20MB for normal use of Java stored procedures. JAVA_SOFT_SESSIONSPACE_LIMIT? Identifies a soft limit on memory used by Java in a session. The default is 1MB. If this limit is exceeded, a warning is written to the ALERT log. JAVA_MAX_SESSIONSPACE_SIZE? Identifies the maximum amount of memory that can be used by a Java procedure; the default is 4GB. When the limit set by this parameter is exceeded, the executing Java procedure is killed by Oracle8i automatically. 如果將JAVA程序存放在數據庫中,并運行存儲在數據庫中的JAVA程序,則數據庫中會(huì )啟用JAVA的虛擬機,為了保證JAVA虛擬機有效的運行,你需要設置上面介紹的參數。
就像前面說(shuō)得,java程序或類(lèi)可以被存儲到數據庫中,作為PL/SQL的替換或補充。Java可以被用來(lái)作為數據庫的觸發(fā)器、存儲過(guò)程、函數、對象的成員函數。在按照下面的過(guò)程開(kāi)發(fā)完java存儲過(guò)程后,就可以從SQL或PL/SQL中調用JAVA存儲過(guò)程,就像調用普通的PL/SQL過(guò)程一樣。下面的代碼描述了如何在SQL*PLUS中開(kāi)發(fā)和使用一個(gè)輸出"Hello, World" 的JAVA程序的例子: 1. Write the Java program using a Java development environment like Jdeveloper or JBuilder. 2. Load the Java program into Oracle8i using either the create or replace java source command, or with the LOADJAVA utility. 3. Publish your Java procedure to SQL. This step identifies your Java procedure to SQL and PL/SQL by exposing the procedure entry point, mapping datatypes in Java to PL/SQL or SQL, and indicating parameter-passing between Java and PL/SQL or SQL.
(1)編寫(xiě)java程序 ---可以直接在SQL*PLUS中創(chuàng )建JAVA的源文件,當然如果有已經(jīng)編譯好的java class,則可以直接跳過(guò)這一步,直接到將java程序發(fā)布出去這一步 SQL> -- first, create the Java source code SQL> create or replace java source named "Hello" as public class Hello { static public String Message(String name) { return "Hello, " + name; } } / Java created. (2)發(fā)布java程序 SQL> -- Now, publish it to SQL SQL> create or replace function hello (name VARCHAR2) return VARCHAR2 as language java name ‘Hello.Message (java.lang.String) return java.lang.String‘; Function created. (3)使用發(fā)布的JAVA程序 SQL> -- Now, you can use the Java procedure from a SQL statement SQL> select hello(‘world!‘) from dual; HELLO(‘world!‘) --------------- Hello world! --- hello函數在8i中不支持中文,9i中支持。如: SQL> select hello(‘你好!‘) from dual; HELLO(‘你好!‘) ------------------ Hello, 你好!