Use PL/SQL to execute OS commands

Source: Internet
Author: User
Plsql is a complementary language for SQL. Its advantage is naturally in data processing. However, with the continuous growth of PLSQL, especially the increasingly close relationship with JAVA

Pl/SQL is a complementary language for SQL, and its advantage is naturally in data processing. However, with the continuous growth of PL/SQL, especially the increasingly close relationship with the JAVA language, this causes

Pl/SQL is a complementary language for SQL, and its advantage is naturally in data processing. However, with the continuous growth of PL/SQL, especially the increasingly close relationship with the JAVA language, this allows PL/SQL to complete some advanced application operations.

We have also introduced how to use PL/SQL to execute java storage to obtain the MAC address.

The method is similar here, but you only need to input command parameters.

Create a java source

Create or replace and compile java source named ExeSysCommand
Import java. io .*;
Public class ExeSysCommand
{
Public static String ExeCmd (String args ){
Runtime run = Runtime. getRuntime ();
Process process = null;
Try {
Process = run.exe c (args); // execute the cmd command
Return "OK! ";
} Catch (Exception e ){
Return e. getMessage ();
}
}
Public static void main (String [] args)
{
System. out. println ("No command line arguments ");
}
}

Then create a function to call the java soure

Create or replace function ExeCmd (cmd STRING) RETURN VARCHAR2 IS -- execute OS command Test
Language java name 'exesyscommand. ExeCmd (java. lang. String) return java. lang. string ';

NOTE: With the cmd parameter, the corresponding java input type should be java. lang. String.

It should also be noted that these operations should be performed under the SYS user. If they are performed under another user, errors will be obtained:

The Permission (java. io. filePermission <> execute) has not been granted to SCOTT. the PL/SQL to grant this is dbms_java.grant_permission ('Scott ', 'sys: java. io. filePermission ',' <> ', 'execute ')

If other users are required to execute the task, you can use dbms_java.grant_permission to grant permissions to the corresponding user.

The following operations are performed in windows:

C: \ Documents ents and Settings \ Administrator> tasklist | find "calc"

First confirm that the system does not currently run the calculator program.

Well, let's use the just-created plsql function execmd to run this calculator program.

C: \ Documents ents and Settings \ Administrator> sqlplus/as sysdba

SQL * Plus: Release 11.2.0.1.0 Production on Wednesday July 10 17:26:17 2013

Copyright (c) 1982,201 0, Oracle. All rights reserved.


Connect:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0-Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

17:26:17 SYS @ orcl> select execmd ('calc') from dual;

EXECMD ('calc ')
Bytes -----------------------------------------------------------------------------------------

OK!

Used time: 00: 00: 00.06
17:26:37 SYS @ orcl> host
Microsoft Windows XP [version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C: \ Documents ents and Settings \ Administrator> tasklist | find "calc"
Calc.exe 12000 Console 0 3,052 K

Well, we can see that the calculator program is already running, but unfortunately, there is no interface...

I guess it may be because Oracle was first developed based on linux and is opposite to MS (for more information, see Oracle's startup history...), so I have not considered supporting WIN interface.

--

However, this is also very convenient. We can create a scheduled JOB and use PL/SQL to call any programs/scripts on the OS for related tasks.

,

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.