Java in Oracle Database Example

In this tutorial, I am giving an example to implement Java code in Oracle Database using the stored function. We can create Java programs in the Oracle database by using CREATE OR REPLACE AND COMPILE JAVA SOURCE statement. After that, we can refer that code in a stored function or a stored procedure with AS LANGUAGE JAVA clause. The following are the steps to implement Java in Oracle Database.

Java in Oracle Database Example

  1. The following is the Java Hello World program example. First, we will add Java code using the CREATE OR REPLACE AND COMPILE AS JAVA SOURCE statement in Oracle database.
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "Hello" AS
public class Hello
{
public static String World()
{
return "Hello World!";
}
};
/
  1. Now create a stored function referring to the above Java program in Oracle database.
CREATE OR REPLACE FUNCTION helloworld
RETURN VARCHAR2
AS
LANGUAGE JAVA
NAME 'Hello.World () return java.lang.String';
/

DECLARE
v_string VARCHAR2 (100 CHAR);
BEGIN
v_string := helloworld ();
END;
/

You can test it now:

SELECT helloworld FROM DUAL;

Output:

HELLOWORLD 
--------------------------
Hello World! 
1 row selected.
Vinish Kapoor
Vinish Kapoor

Vinish Kapoor is a seasoned software development professional and a fervent enthusiast of artificial intelligence (AI). His impressive career spans over 25+ years, marked by a relentless pursuit of innovation and excellence in the field of information technology. As an Oracle ACE, Vinish has distinguished himself as a leading expert in Oracle technologies, a title awarded to individuals who have demonstrated their deep commitment, leadership, and expertise in the Oracle community.

guest

4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments