PL/SQL程序中调用Java代码(转)

 

主要是学习PL/SQL调用JAVA的方法.

平台:WINDOWS

1.首先使用IDE写好需要调用的java代码,再添加"create or replace and compile java source named 名字 as",然后在PL/SQL中执行

 

create or replace and compile java source named getosmac as

import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class GetOSMac {
    /**
     * 获取MAC地址
     */
    public static String getMac() {
        String result = "";
        try {
            Process process = Runtime.getRuntime().exec("ipconfig /all");
            /**
             * 通过执行dos命令IPCONFIG / ALL来获取
             */
            InputStreamReader ir = new InputStreamReader(
                    process.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);

            String line;

            while ((line = input.readLine()) != null)

                if (line.indexOf("Physical Address") > 0) {

                    String macAddr = line.substring(line.indexOf("-") - 2);

                    result = macAddr;

                }
        } catch (java.io.IOException e) {
            System.err.println("IOException " + e.getMessage());
        }

        System.out.println("Mac:" + result);
        return result;
    }

    public static void main(String[] args) {
        getMac();
        System.out.println("OK!!!");
    }

}

2.然后创建函数调用该JAVA SOURCE的Fuction

create or replace function GetMac return varchar2 is
  language java name ' GetOSMac.getMac() return java.lang.String'; 

3.调用函数.

SYS@orcl> select GetMac AS MAC from dual;  
  
MAC  
------------------------------------------------  
  
50-E5-49-17-EC-82  
  
已选择 1 行。  

http://blog.csdn.net/edcvf3/article/details/8771453

常见错误及解决办法:

问题一:
ERROR at line 1: ORA-29538: Java not installed

解决方法
1.检查有没有安装JAVA组件
select * from v$option t where t.PARAMETER='Java';
如果返回行说明已安装,如果没有返回行,运行Oracle Universal Installer安装JAVA组件
2.如果在第1步返回行,则检查oracle中有没有dbms_java.
select distinct owner,name from dba_source where lower(NAME)='dbms_java';
如果没有返回行,执行第3步
3.在sqlplus下以sys登陆,执行$ORACLE_HOME/javavm/install/initjvm.sql
SQL>@?/javavm/install/initjvm.sql;

问题二:
ERROR at line 1:
ORA-29532: Java call terminated by uncaught Java exception:
java.security.AccessControlException: the Permission (java.io.FilePermission
/home/accmgrctl/src/server read) has not been granted to SQLVIEW. The PL/SQL to
grant this is dbms_java.grant_permission( 'SQLVIEW',
'SYS:java.io.FilePermission', '/home/accmgrctl/src/server', 'read' )
ORA-06512: at "SQLVIEW.PKG_FILE_API", line 1
解决方法:
这是由于oracle用户没有访问那个目录的权限,把源代码入在oracle有权限访问的目录下
或者用以下语句授权
EXEC Dbms_Java.Grant_Permission('oracle_username','java.io.FilePermission', '*','read ');
 

 

 
问题三:
ERROR at line 1:
ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-00552: internal XAD package failed to load
ORA-06512: at "SYS.ORACLE_LOADER", line 19
解决方法:
这是110202 上的又一新BUG(外部表的读取)
Need to replace the language specific (non-english) kup<lang>.msb file 
with the english version.

1. cd $ORACLE_HOME/rdbms/mesg
2. Replace <lang> with your installed languages file.
mv KUP<lang>.msb to KUP<lang>.msb.BAK
3. Copy <us> version over current <lang> copy of kup msb file.
cp kupus.msb to KUP<lang>.msb
4. re-run the select against the external table

我实际的操作过程,就是:

该目录:$ORACLE_HOME/rdbms/mesg 下有两个文件:
kupzhs.msb 和 kupus.msb, 其默认使用了kupzhs.msb
此时,我把kupzhs.msb 重新命名为 kupzhs.msb.bak
之后在测试, OK: 
http://blog.csdn.net/liulunxu/article/details/8494556
http://bbs.csdn.net/topics/280052330
http://alvin.blog.51cto.com/210283/91178
 

create or replace and compile java source named getosmac as
import java.io.InputStreamReader;import java.io.LineNumberReader;
public class GetOSMac {    /**     * 获取MAC地址     */    public static String getMac() {        String result = "";        try {            Process process = Runtime.getRuntime().exec("ipconfig /all");            /**             * 通过执行dos命令IPCONFIG / ALL来获取             */            InputStreamReader ir = new InputStreamReader(                    process.getInputStream());            LineNumberReader input = new LineNumberReader(ir);
            String line;
            while ((line = input.readLine()) != null)
                if (line.indexOf("Physical Address") > 0) {
                    String macAddr = line.substring(line.indexOf("-") - 2);
                    result = macAddr;
                }        } catch (java.io.IOException e) {            System.err.println("IOException " + e.getMessage());        }
        System.out.println("Mac:" + result);        return result;    }
    public static void main(String[] args) {        getMac();        System.out.println("OK!!!");    }
}

 

posted @ 2015-10-16 15:38  沧海一滴  阅读(1811)  评论(0编辑  收藏  举报