Java programs or classes can be stored in the database and replaced or supplemented with PL/SQL. Java can be used as a database trigger, stored procedure, function, and object member function. After developing a Java stored procedure according to the following procedure, you can call the Java stored procedure from SQL or PL/SQL, just like calling a common PL/SQL process. The following code describes how to develop and use a Java program that outputs "Hello, world" in SQL * Plus:
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) Write a Java program
--- You can directly create Java source files in SQL * Plus. Of course, if a compiled Java class exists, you can skip this step and release the Java program directly.
SQL> -- first, create the Java source code
SQL> Create or replace Java source named "hello"
Public class Hello {
Static Public String message (string name ){
Return "hello," + name;
}
}
/
Java created.
(2) Release Java programs
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) Use the released Java program
SQL> -- now, you can use the Java procedure from a SQL statement
SQL> select Hello ('World! ') From dual;
Hello ('World! ')
---------------
Hello world!
--- The hello function does not support Chinese Characters in 8i and 9i. For example:
SQL> select Hello ('Hello! ') From dual;
Hello ('Hello! ')
------------------
Hello!