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的客户端机器上安装客户端软件。 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
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有关的初始化参数:====================================== 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程序,则数据库中会启用JAVA的虚拟机,为了保证JAVA虚拟机有效的运行,你需要设置上面介绍的参数。
如何将一个JAVA程序装载到数据库并且发布出去?===================================
就像前面说得,java程序或类可以被存储到数据库中,作为PL/SQL的替换或补充。Java可以被用来作为数据库的触发器、存储过程、函数、对象的成员函数。在按照下面的过程开发完java存储过程后,就可以从SQL或PL/SQL中调用JAVA存储过程,就像调用普通的PL/SQL过程一样。下面的代码描述了如何在SQL*PLUS中开发和使用一个 输出"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)编写java程序 ---可以直接在SQL*PLUS中创建JAVA的源文件,当然如果有已经编译好的java class,则可以直接跳过这一步,直接到将java程序发布出去这一步 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)发布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)使用发布的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, 你好! | |