我试图将Java String数组传递给PLSQL Stored-Procedure。经过大量的互联网搜索,我找到了一个对我不起作用的解决方案。 首先,这里是我用PL / SQL表声明的包,名为" CHAR_TABLE"
包装:
create or replace PACKAGE MYPACKAGE IS
TYPE CHAR_TABLE IS TABLE OF CHAR(06) INDEX BY BINARY_INTEGER;
PROCEDURE MYPROCEDURE(
p_strings CHAR_TABLE
);
END MYPACKAGE;
这是身体(也不重要): 创建或替换PACKAGE BODY MYPACKAGE是
PROCEDURE MYPROCEDURE(
p_strings CHAR_TABLE
) AS
BEGIN
FOR i IN 1..p_strings.COUNT
LOOP
INSERT INTO myTable (s) VALUES (p_strings(i));
END LOOP;
END MYPROCEDURE;
END MYPACKAGE;
如果在互联网上找到的Java代码如下:
OracleCallableStatement stmt = (OracleCallableStatement)oracleConnection.prepareCall("{call MYPACKAGE.MYPROCEDURE(?)}");
String[] data = { "one", "two", "three" };
Array array = oracleConnection.createOracleArray("MYPACKAGE.CHAR_TABLE", data);
stmt.setArray(1, array);
stmt.execute();
conn.commit();
conn.close();
但问题是我得到以下异常:
Exception in thread "main" java.sql.SQLException: ORA-01403: no data found
ORA-06512: at "BERNHARD.MYPACKAGE", line 9
ORA-06512: at line 1
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1017)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:655)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:249)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:566)
at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:210)
at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:53)
at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:938)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1075)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3820)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3923)
at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:5617)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1385)
仅用于测试(查看对StoredProcedure的调用是否有效)我尝试了以下代码:
OracleCallableStatement stmt = (OracleCallableStatement)oracleConnection.prepareCall(" DECLARE var BERNHARD.MYPACKAGE.CHAR_TABLE;\n" +
"BEGIN\n" +
"\n" +
"var(1) := 'a';\n" +
"var(2) := 'b';\n" +
"MYPACKAGE.MYPROCEDURE(var);\n" +
"END;");
stmt.execute();
这很有效。但是我需要第一个将Java String Array绑定到Stored Procedure的输入参数的解决方案。我怎样才能做到这一点?或者我的错误是什么?
答案 0 :(得分:1)
我猜测a-array(关联数组)是否来自java。它的索引从0开始。 您可以尝试更改循环范围或更改循环类型。对于a-array,更合适的是while循环。 检查示例。
declare
type CHAR_TABLE IS TABLE OF CHAR(06) INDEX BY BINARY_INTEGER;
p_strings CHAR_TABLE;
idx BINARY_INTEGER;
begin
p_strings(0) := 'aaaa';
p_strings(1) := 'bbbb';
p_strings(2) := 'cccc';
-- 1-st option
FOR i IN 0..p_strings.last loop
dbms_output.put_line(p_strings(i));
end loop;
-- 2-nd option
idx := p_strings.first;
while idx is not null
loop
dbms_output.put_line(p_strings(idx));
idx := p_strings.next(idx);
end loop;
end;