From: http://hi.baidu.com/hexiong/blog/item/a6464954848d6c50574e006c.html
In the future, Java stored procedures will become more and more popular among major database manufacturers and become more and more powerful. The following uses oracle as an example to describe how to use Java stored procedures.
Respect copyright for any reposted content. (Author: iihero on csdn)
1. How to Create a Java stored procedure?
There are usually three methods to create a Java stored procedure.
1. Use Oracle SQL statements to create:
E.g. Use create or replace and compile Java source named "<Name>"
Keep up with the Java sourceProgram. The class method must be public static before it can be used for stored procedures.
SQL > Create Or Replace And Compile Java source named "javademo1"
2 As
3 Import java. SQL. * ;
4 Public Class javademo1
5 {
6 Public Static void main (string [] Argv)
7 {
8 System. Out. println ("Hello, Java demo1 ");
9 }
10 }
11 /
Java has been created.
SQL > Show Errors Java source "javademo1"
No error.
SQL > Create Or Replace Procedure Javademo1
2 As
3 Language Java name ' Javademo1.main (Java. Lang. String []) ' ;
4 /
The process has been created.
SQL > Set Serveroutput On
SQL > Call javademo1 ();
The call is complete.
SQL > Call dbms_java.set_output ( 5000 );
The call is complete.
SQL > Call javademo1 ();
Hello, Java demo1
The call is complete.
SQL > Call javademo1 ();
Hello, Java demo1
The call is complete.
2. Use an external class file to load and create
E.g. Since external files are used, the class files must be placed under a directory of Oracle server.
Public Class oraclejavaproc
{
Public Static void main (string [] Argv)
{
System. Out. println ("it ' S a Java Oracle procedure .");
}
}
SQL> grant create any directory to Scott;
Authorization successful.
SQL> conn Scott/tiger@iihero.oracledb
Connected.
SQL> Create or replace directory test_dir ' D: \ oracle ' ;
The directory has been created.
SQL> Create or replace Java class using bfile (test_dir, ' Oraclejavaproc. Class ' )
2/
Java has been created.
SQL> Create or replace procedure testjavaproc as language Java name ' Oraclejavaproc. Main (Java. Lang. String [] ) ' ;
2/
The process has been created.
SQL> call testjavaproc ();
The call is complete.
SQL> execute testjavaproc;
The PL/SQL process is successfully completed.
SQL> set serveroutput on size 5000
SQL> call dbms_java.set_output (5000 );
The call is complete.
SQL> execute testjavaproc;
It ' S a Java Oracle Procedure .