Created
April 28, 2021 03:31
-
-
Save hemantkchitale/e0d29400c5bbd9b85fdefad7fae40e5c to your computer and use it in GitHub Desktop.
Loading Java code into OJVM for execution in an Oracle Database Session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- see demo at https://hemantoracledba.blogspot.com/2021/04/ojvm-loading-java-code-and-running-it.html | |
-- grant Java Code permission to only READ only /home/oracle/tmp | |
-- this should have been provided by the DBA | |
begin | |
dbms_java.grant_permission('HEMANT', | |
'SYS:java.io.FilePermission', | |
'/home/oracle/tmp', 'read'); | |
dbms_java.grant_permission( 'HEMANT', | |
'SYS:java.io.FilePermission', | |
'/home/oracle/tmp/*', | |
'read' ); | |
dbms_java.grant_permission( 'HEMANT', | |
'SYS:java.lang.RuntimePermission', | |
'getFileSystemAttributes', | |
'' ); | |
end; | |
/ | |
-- here is where I load the Java code into my schema | |
connect hemant/hemant@orclpdb1 | |
-- load the java code into the database | |
create or replace and compile java source named "readOSDirectory" | |
as | |
/* using java.io */ | |
import java.io.*; | |
/* create the main class */ | |
public class readOSDirectory | |
{ | |
/* create the class to be executed from a procedure */ | |
public static void getList(String directory) | |
{ | |
/* use File class from java.io */ | |
File directoryPath = new File( directory ); | |
File filesList[] = directoryPath.listFiles(); | |
/* read till end of list */ | |
for (File file : filesList) | |
{ | |
System.out.println("File Name: "+file.getName() | |
+ " File Size: "+file.length()); | |
} | |
} | |
} | |
/ | |
/* create a PL/SQL procedure to call the getList class */ | |
create or replace | |
procedure Read_Directory( p_directory in varchar2 ) | |
as language java | |
name 'readOSDirectory.getList( java.lang.String )'; | |
/ | |
/* enable output to screen-- serveroutput is for my sqlplus session */ | |
SET SERVEROUTPUT ON SIZE 100000 | |
CALL dbms_java.set_output (100000); | |
begin | |
Read_Directory('/home/oracle/tmp'); | |
end; | |
/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment