|
Jay ,
You need a java class loaded into database Which
runs OS Commands (or .bat file) . For this look at below... ...
Hope this helps . (But I could not manage to run OS
commands deleting files from network altough shared.But locally it works
fine.)
Bunyamin K.
Karadeniz
Oracle DBA / Developer Civilian IT Department Havelsan A.S. Eskisehir yolu 7.km Ankara Turkey Phone: +90 312 2873565 / 1217 Step1. compile the java code javac program_name.java This operation is used to generate related class. Do not need to recompile java code, if the related class is exists. Step2. Load generated class into the Database loadjava –u user_name/password@connection_name program_name.class - Drop java class from the database : dropjava –u user_name/password@connection_name program_name.class - Publish the Java code to PL/SQL by creating the PL/SQL wrapper CREATE OR REPLACE PROCEDURE executecmd (S1 VARCHAR2) AS LANGUAGE JAVA name ‘program_name.main(…)’ - Call the procedure set serveroutput on call dbms_java.set_output(2000) /*obtional*/ exec program_name Example : Call an OS command from a Java stored procedure (JSP) -------------------code begins here------------------------------------ import java.lang.Runtime; import java.lang.Process; import java.io.IOException; import java.lang.InterruptedException; class ExecuteCmd { public static void main(String args[]) { System.out.println("In main"); try { /* Execute the command using the Runtime object and get the Process which controls this command */ Process p = Runtime.getRuntime().exec(args[0]); /* Use the following code to wait for the process to finish and check the return code from the process */ try { p.waitFor(); /* Handle exceptions for waitFor() */ } catch (InterruptedException intexc) { System.out.println("Interrupted Exception on waitFor: " + intexc.getMessage()); } System.out.println("Return code from process"+ p.exitValue()); System.out.println("Done executing"); /* Handle the exceptions for exec() */ } catch (IOException e) { System.out.println("IO Exception from exec : " + e.getMessage()); e.printStackTrace(); } } } -----------------code ends here------------------------------------------ You can write this code with any editor and save it as "executecmd.java ". Then compile this code. If you do not have any java editor, you can also compile this program using "command prompt" as follows :
set PATH=%PATH%; path_name
javac program_name.java After compile operation you can load this codes into the database and execute it as described above. NOTE : Some permissions are necessary to execute java codes into the database, this operations can be made as follows : - connect database with system manager (conn sys/password@connectionname ) - set serveroutput on - call dbms_java.grand_permission(username,permissionname, filename, permissions) call dbms_java.grand_permission(‘user’,’java.io.filePermission’,’*’,’read,write,execute’) call dbms_java.grand_permission(‘user’,’java.lang.runtimePermission’,’*’,writeFileDescriptor’) ….. - commit - log of and reconnect as the specified user You can also give all permissions to the specified user with this command : grant JAVASYSPRIV to username if no permissions are given to the user, the procedure are not executed correctly.
|
- Re: Using Java Blocks Can Anyone Help? Bunyamin K. Karadeniz
