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 source Program . The class method must be public static before it can be used for stored procedures.
CopyCode The Code is as follows: SQL> Create or replace and compile Java source named "javademo1"
2
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
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.Copy codeThe Code is as follows: 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 as '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.