Sunday, October 9, 2016

Creating a Java program in Oracle Database

Creating a Java program in Oracle Database

We can create and implement java sources directly on Oracle database so we can invoke them by PL/SQL or other interface.

Follow below steps
Create user user1 identified by password Grant connect, resource to user1
For example :
This java stored procedure will list gived directory files create or replace and compile java source named "getdirlisting" as import java.io.*; import java.util.*; import java.util.zip.*; import java.text.*; import java.lang.*; class getdirlisting { public static String getlisting(String dirname)
throws Exception { String retval=null; File dir = new File(dirname); String files = dir.list(); if (files!=null) { for (int i=0;i -- plsql program that will invoke the java stored procedure create or replace function getfilesindir(p_long_path_dir_name varchar2) return varchar2 as language java name 'getdirlisting.getlisting(java.lang.String) return java.lang.String'; / -- procedure for testing -- in this case we'll just dbms_output the file names, we can insert the list into a table create or replace procedure processDir(dirname varchar2) as filelist myTableType;
begin
filelist := str2tbl(getfilesindir(dirname));
for i in filelist.first .. filelist.last
LOOP
dbms_output.put_line('processing ' || filelist(i));
END LOOP;
end; /

 -- test it.
set serveroutput on

exec processDir('c:\mydir');