Using Java to write stored procedures for Oracle databases

Source: Internet
Author: User
Tags database join

Oracle can use multiple languages to write stored procedures, such as Pro*c/c++,pl/sql,cobol, and in oracle8i to support writing stored procedures in Java.

If you do not want to write stored procedures, as a living Java, I prefer to write in Java, with pl/sql need to memorize a lot of grammar (Pascal class syntax) and functions, far less than using JAVA/JDBC so familiar. Also, databases like DB2 support Java stored procedures, so it's no better to learn a way to write stored procedures for each type of data.

The Java stored procedures differ from the general JDBC programs:

1. Security restrictions, after all, are running within Oracle, not allowing access to operating system resources, such as files.

2. Get database join mode, connection = new Oracledriver (). DefaultConnection ();

3.system.out,system.err,system.in input and output are different. You can take advantage of some command redirection.

Here's an example of Hello world with a Java stored procedure.

1 in Plsqldeveloper, add a TESTJAVA1 class to Java source,

Code

create or replace and compile java source named TestJava1 as
public class TestJava1
{
public static void test()
{
System.out.println("Hello");
}
}

Execute it to save and compile.

2) Add a procedure and execute the following command:

Code

create or replace procedure testJava1
as
language java name 'TestJava1.test()';

3 Enter in command window

Code

SET SERVEROUTPUT ON;
CALL dbms_java.set_output(2000);

To redirect the System.out to the current window;

Enter in command window, exec testJava1 ();

You can see the result:

Hello

Pl/sql procedure successfully completed

Related Article

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.