就像前面說得,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 Oracle
8i 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, 你好!