从Oracle函数调用Java函数

时间:2018-06-13 19:45:47

标签: java oracle stored-procedures

我在Oracle中创建了Java类,但不确定如何创建函数来返回值。任何帮助,将不胜感激。

create or replace and compile java source named "ErrorMessage" 
as 
 import java.util.zip.Inflater;
 public class ErrorMessage {
    public static String uncompressMessage(byte[] cdata, int        uncompressedLength){
        String uncompData2 = null;
         try{

            Inflater inflater = new Inflater();
            inflater.setInput(cdata);
            Byte [] uncompData = new byte[uncompressedLength];
            int n = inflater.inflate(uncompData);
      uncompData2 = uncompData.toString();
            inflater.end();

        }catch (Exception e){
            e.getMessage();
        }
        return uncompData2;
    }
}

create or replace 
function getErrorMessage(msgdata blob, msgdatalength integer)
return varchar2
as language java
name 'ErrorMessage.uncompressMessage(byte[],int)
return java.lang.String';

1 个答案:

答案 0 :(得分:1)

类似的东西:

create or replace and compile java source named "ErrorMessage" 
as 
  import java.util.zip.Inflater;
  import java.lang.Byte;
  import java.sql.Blob;

  public class ErrorMessage {
    public static String uncompressMessage( Blob data, int uncompressedLength ){
      String uncompData2 = null;
      try{
        final long size = data.length();
        final byte[] cdata = data.getBytes( 0L, (int) size );
        Inflater inflater = new Inflater();
        inflater.setInput( cdata );
        byte[] uncompData = new byte[uncompressedLength];
        int n = inflater.inflate(uncompData);
        uncompData2 = new String( uncompData, "UTF-8" );
        inflater.end();
      }catch (Exception e){
        e.getMessage();
      }
      return uncompData2;
    }
}
/

create or replace function getErrorMessage(msgdata blob, msgdatalength number) return varchar2
as language java
name 'ErrorMessage.uncompressMessage(java.sql.Blob,int) return java.lang.String';