如何在Oracle 10gR2中调用Java存储过程?

时间:2016-05-26 16:25:02

标签: java oracle java-stored-procedures

这是我的代码:

SET DEFINE OFF;

CREATE OR REPLACE AND COMPILE NOFORCE JAVA SOURCE NAMED "SCHEMA"."DigestUtils" AS
/* imports here... */

public class DigestUtils {

    private static final char[] hexCode = "0123456789ABCDEF".toCharArray();
    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

    public static String sha512Hex(Clob c) throws MyException {
        // Code here ...
        return "Hex.string.here";
    }

    private static class MyException extends Exception {
        private static final long serialVersionUID = 8501244872025707585L;

        public MyException(Throwable cause) {
            super(cause);

            if ((cause instanceof SQLException) && !(cause instanceof SQLWarning) && (DriverManager.getLogWriter() != null)) {
                printStackTrace(DriverManager.getLogWriter());
            }
        }
    }
}
;

CREATE OR REPLACE FUNCTION sha512Hex RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'DigestUtils.sha512Hex(java.sql.Clob) return java.lang.String';

当我尝试从SQL调用Java存储过程时,如下所示:

select  clob_column, sha512Hex(clob_column)
from my_table
where id in (49917,49918,49919,60455)

我收到此错误:

ORA-06553: PLS-306: numéro ou types d'arguments erronés dans appel à 'sha512Hex'
06553. 00000 -  "PLS-%s: %s"
*Cause:    
*Action:
Error on line 196, column 17 (translated from original message)

以下是错误行:

(l. 196) select  clob_column, sha1Hex(clob_column)
                              ^
                              |___ column 17

我错过了什么?

Oracle 10gR2
Java 1.4.2(嵌入在Oracle中)

2 个答案:

答案 0 :(得分:3)

如果您描述当前的功能,您会看到:

desc sha512Hex

Argument Name  Type     In/Out Default 
-------------- -------- ------ ------- 
<return value> VARCHAR2 OUT    unknown 

您的PL / SQL函数声明也需要定义参数及其类型:

CREATE OR REPLACE FUNCTION sha512Hex(c CLOB) RETURN VARCHAR2 AS
LANGUAGE JAVA NAME 'DigestUtils.sha512Hex(java.sql.Clob) return java.lang.String';

desc sha512Hex

Argument Name  Type     In/Out Default 
-------------- -------- ------ ------- 
<return value> VARCHAR2 OUT    unknown 
C              CLOB     IN     unknown 

然后,您可以在尝试提问时调用它。

答案 1 :(得分:1)

此答案完成Alex Poole's one

以下是使用纯SQL查询描述函数的方法:

SELECT
     argument_name
    ,pls_type
    --,data_type -- may contain useful info
    ,in_out
    ,default_value
FROM
   user_arguments -- replace with ALL_ARGUMENTS for functions you don't own
WHERE
   object_name = 'SHA1HEX'
ORDER BY 
   position

输出

ARGUMENT_NAME    PLS_TYPE       IN_OUT    DEFAULT_VALUE
---------------- -------------- --------- --------------
<null>           VARCHAR2       OUT       <null>
C                CLOB           IN        <null>

参考