Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> Re: Using Java Blocks Can Anyone Help?

Re: Using Java Blocks Can Anyone Help?

From: Jay Wade <fish_dba_at_hotmail.com>
Date: Mon, 28 Jan 2002 07:26:22 -0800
Message-ID: <F001.003FD209.20020128070551@fatcity.com>

Bunyamin thanks for your help. The example is great I just have a few follow up questions. Please keep in mind that I am very much a newbie to all things Java. If I wasn't able to store the objects in the database could I put all of the below into a script which would create the objects then drop them? If so would I have to compile each time or can I just reference the path? Also, due to my very limited experience with Java does the below included code allow me to delete an operating system file? If so what would the syntax, del C:\temp\filename.txt (same as MS DOS)?

>From: "Bunyamin K. Karadeniz" <bunyamink_at_havelsan.com.tr>
>Reply-To: ORACLE-L_at_fatcity.com
>To: Multiple recipients of list ORACLE-L <ORACLE-L_at_fatcity.com>
>Subject: Re: Using Java Blocks Can Anyone Help?
>Date: Mon, 28 Jan 2002 05:00:26 -0800
>
>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_at_connection_name program_name.class
>
>
>- Drop java class from the database :
>
>dropjava -u user_name/password_at_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 :
>
> a.. define the path where the necessary classes and packages are hold
>set PATH=%PATH%; path_name
>
> a.. compile the program
>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_at_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.
>
>
>
>
>
>
>
> ----- Original Message -----
> From: Jay Wade
> To: Multiple recipients of list ORACLE-L
> Sent: Monday, January 28, 2002 1:55 PM
> Subject: OT: Using Java Blocks Can Anyone Help?
>
>
> hello:
> Can you run a Java Store Procedure as an anonymous block? I'm trying
>to do
> as suggested and use Java to delete an operating system file
>(Win2k/NT).
> The only issue is that this must be executed in script format without
> storing any objects or references in the database. Since I am using
>8.1.6
> I
> figured that I might be able to do it all as a sql file with anonymous
>Java
> blocks. Can this be done? If do does anyone have an example of the
>syntax
> that would be needed to delete a file from a passed parameter? I've
> started
> experimenting with the below but so far have been unsuccessful, in
>getting
> the block to execute. Unfortunately I know very little Java so any
>help or
> directory would be greatly appreciated.
>
> Begin
> public class Hello { /* Pure Java
>Code
> */
> static public String Msg(String tail) {
> return "Hello " + tail;
> }
> }
>
> Thanks in advance,
> Jay
>

I no longer need to punish, deceive, or compromise myself, unless I want to stay employed.



Join the world’s largest e-mail service with MSN Hotmail. http://www.hotmail.com
-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.com
-- 
Author: Jay Wade
  INET: fish_dba_at_hotmail.com

Fat City Network Services    -- (858) 538-5051  FAX: (858) 538-5051
San Diego, California        -- Public Internet access / Mailing Lists
--------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: ListGuru_at_fatcity.com (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).
Received on Mon Jan 28 2002 - 09:26:22 CST

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US