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.
Copy codeThe 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.