简体   繁体   中英

Passing byte array as parameter to stored procedure in oracle

I have function, which I need to put in Oracle Database (I'm using 11g) as stored procedure. Suppose that this function looks like this:

public static BLOB useByteArray(byte[] byteArray){
    //do something with this byte array, return BLOB with something
}

So how should wrapper function looks? I know it will be something around this

CREATE OR REPLACE FUNCTION USE_BYTE_ARRAY(byteArray ???) RETURN BLOB IS
 LANGUAGE JAVA NAME 'com.example.something.useByteArray(byte[]???)';

but I have no idea how can I pass this byte array between wrapper and java function. Perhaps, encode it in Base64, pass as string and then encode in useByteArray method?

Thanks in advance :)

我猜您可以使用OracleTypes.BLOB从Java应用程序传递数据,并且可以在存储过程中使用blob数据类型使用它。

For this kind requirement, you can use SYS REF CURSOR from oracle. We can send ARRAY of values to stored procedure.

1) SYS REF CURSOR Declaration:

CREATE OR REPLACE PACKAGE TEST_CURSOR AS

TYPE testCursorType is REF CURSOR; END;

2) Create ORACLE type of table for accepting ARRAY

CREATE OR REPLACE TYPE tabletype AS TABLE OF NUMBER(10);

3) Create your procedure

 CREATE OR REPLACE PROCEDURE testProc
(adeptno tabletype,
testFetch IN OUT test_cursor.testCursorType) AS
BEGIN
OPEN testFetch FOR 
SELECT * 
FROM emp
WHERE deptno IN (SELECT *
FROM TABLE(CAST(adeptno AS tabletype)));
END;

4) And finally JAVA code

private static final String ARRAY_PROCEDURE = "call testProc(?,?)";
int arrayElements[] = {10,20,30,40};
//Create an Array Descriptor
ArrayDescriptor deptdesc = ArrayDescriptor.createDescriptor("TABLETYPE",connObj);
//Define the Array (Descriptor,connection, Elements)
ARRAY deptarray = new ARRAY(deptdesc, connObj, arrayElements);
this.executePrepareQuery(conn, ARRAY_PROCEDURE,deptarray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM