简体   繁体   English

将字节数组作为参数传递给oracle中的存储过程

[英]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. 我有功能,需要将其作为存储过程放入Oracle数据库(我正在使用11g)中。 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. 但是我不知道如何在包装器和Java函数之间传递此字节数组。 Perhaps, encode it in Base64, pass as string and then encode in useByteArray method? 也许,在Base64中对其进行编码,作为字符串传递,然后在useByteArray方法中进行编码?

Thanks in advance :) 提前致谢 :)

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

For this kind requirement, you can use SYS REF CURSOR from oracle. 对于这种要求,可以使用oracle的SYS REF CURSOR。 We can send ARRAY of values to stored procedure. 我们可以将值的数组发送到存储过程。

1) SYS REF CURSOR Declaration: 1)SYS REF CURSOR声明:

CREATE OR REPLACE PACKAGE TEST_CURSOR AS

TYPE testCursorType is REF CURSOR; TYPE testCursorType为REF CURSOR; END; 结束;

2) Create ORACLE type of table for accepting ARRAY 2)创建ORACLE类型的表来接受ARRAY

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

3) Create your procedure 3)建立程序

 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 4)最后是JAVA代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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