📜 ⬆️ ⬇️

Oracle shell call alternative method

After reading the article Calling a shell from Oracle

I decided to share my solution to this problem. To solve the problem, JavaB is used, so this solution will not work on Oracle XE.

But this solution is cross-platform - it works on all operating systems with which I have come across.
It also allows you to see the results of executing an external command in DBMS_OUTPUT.

Let's start the implementation:
')
Step 1: Create a Java based source.

import java.lang. *;
import java.io. *;
import java.sql.SQLException;

public class Host {

public static void executeCommand (String command) throws SQLException, IOException
{
#sql {
begin
DBMS_JAVA.set_output (10,000,000);
end;
};
String uFullCommand = ""; String nameOS = System.getProperty ("os.name");

if ((nameOS.toLowerCase ()). indexOf ("lin")! = -1)
{
uFullCommand = "/ bin / sh" + "-c" + command;
}
else if ((nameOS.toLowerCase ()). indexOf ("win")! = -1)
{
uFullCommand = "% systemroot% \\ system32 \\ cmd.exe" + "/ y" + "/ c" + command;
}
else if ((nameOS.toLowerCase ()). indexOf ("nix")! = -1)
{
uFullCommand = command;
}
else
{
uFullCommand = command;
}

Runtime rt = Runtime.getRuntime ();
Process p = rt.exec (uFullCommand);
BufferedReader in = new BufferedReader (new InputStreamReader (p.getInputStream ()));

String line = null;
while ((line = in.readLine ())! = null)
{
System.out.println (line);
}
}
};

Step 2: Wrap in PL / SQL.

CREATE OR REPLACE
PROCEDURE host_command_proc (p_command IN VARCHAR2) AS
LANGUAGE JAVA
NAME 'Host.executeCommand (java.lang.String)';
/

Call example:

begin
host_command_proc ("ls / tmp");
end;

And yes, the idea of ​​this solution was borrowed from Tom Kite.

Source: https://habr.com/ru/post/150081/


All Articles