function etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster
function etiketine sahip kayıtlar gösteriliyor. Tüm kayıtları göster

19 Aralık 2010 Pazar

Calling Unix command from Oracle database with java

We can use java to calling unix command from database.

there are 2 object for this purpose.


--create java object with below source.
CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "unixCommand" AS
import java.io.*;
public class unixCommand{

public static String Run(String Command){

try {
String strReturn = "";
Process proc = Runtime.getRuntime().exec(Command);

BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
String output = "";
String currentLine = null; 
while((currentLine = in.readLine()) != null) { 
output += currentLine + "\n";
}
in.close();

if(output != "") 
strReturn = "[O]" + output;

in = new BufferedReader(new InputStreamReader(proc.getErrorStream()));  
String error = "";
currentLine = null; 
while((currentLine = in.readLine()) != null) {  
error += currentLine + "\n";  

}
in.close();

if(error != "") 
strReturn += "[E]" + error;

return strReturn;

} catch (Exception e){
System.out.println("[E]Error running command: " + Command + "\n" + e.getMessage());
return(e.getMessage());
}
}
}
-- create db function.
CREATE or REPLACE FUNCTION unixCommand_Run(Command IN STRING)
RETURN VARCHAR2 IS
LANGUAGE JAVA
NAME 'unixCommand.Run(java.lang.String) return java.lang.String';

--sample
select unixCommand_Run('ls -l') from dual;