如何在 Oracle 10gR2 中调用 Java 存储过程?
How To Call a Java Stored Procedure In Oracle 10gR2?
这是我的代码:
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
我错过了什么?
甲骨文 10gR2
Java 1.4.2(嵌入在 Oracle 中)
如果您描述当前的功能,您会看到:
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
然后您可以像您在问题中尝试的那样调用它。
此回答完成。
以下是如何使用普通 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>
参考资料
这是我的代码:
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
我错过了什么?
甲骨文 10gR2
Java 1.4.2(嵌入在 Oracle 中)
如果您描述当前的功能,您会看到:
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
然后您可以像您在问题中尝试的那样调用它。
此回答完成
以下是如何使用普通 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>