就像前面说得,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, 你好!