How to use Java stored procedures in Oracle (details)

Source: Internet
Author: User

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 .

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.