Search code examples
javaoraclejava-stored-procedures

How To Call a Java Stored Procedure In Oracle 10gR2?


Here is my code:

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';

when I try to call the Java stored procedure from SQL like below:

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

I get this error:

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)

Here is the error line:

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

What am I missing?

Oracle 10gR2
Java 1.4.2 (embedded in Oracle)


Solution

  • If you describe your current function you'll see:

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

    Your PL/SQL function declaration needs to define the argument and its type too:

    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 
    

    You can then call that as you were attempting to in your question.