如何使用关联数组调用PL / SQL过程?

时间:2019-06-24 19:30:00

标签: java oracle jdbc plsql

我正在按照documentation here调用带有关联数组的PL / SQL过程。

尽管我相信这是我要通过的内容,但我却遇到了一个异常array bind type must match PL/SQL table row type

具体来说,我有一个表,PL / SQL包和JDBC代码,如下所示:


表雇员

CREATE TABLE EMPLOYEE 
(   
"EMPLOYEE_NUMBER" VARCHAR2(20 BYTE), 
"EMPLOYEE_NAME" VARCHAR2(80 BYTE), 
"EMPLOYEE_DEPT" VARCHAR2(40 BYTE)
);

包装PKG_SP_EMPLOYEE

create or replace PACKAGE PKG_SP_EMPLOYEE
AS
TYPE tbl_employees_in
IS
TABLE OF EMPLOYEE%ROWTYPE INDEX BY PLS_INTEGER;

PROCEDURE main(
    p_employee IN PKG_SP_EMPLOYEE.tbl_employees_in,
    x_status OUT VARCHAR 
);
END PKG_SP_EMPLOYEE;

包装体PKG_SP_EMPLOYEE

create or replace PACKAGE BODY PKG_SP_EMPLOYEE
AS

PROCEDURE main(
    p_employee IN PKG_SP_EMPLOYEE.tbl_employees_in,
    x_status OUT VARCHAR
    )
IS
BEGIN 
FOR i in 1..p_employee.count

    loop
    INSERT INTO "EMPLOYEE"
           (
           EMPLOYEE_NUMBER, 
           EMPLOYEE_NAME, 
           EMPLOYEE_DEPT
           )
    VALUES (
           p_employee(i).EMPLOYEE_NUMBER,
           p_employee(i).EMPLOYEE_NAME,
           p_employee(i).EMPLOYEE_DEPT
           );

    end loop;

    dbms_output.put_line('Success');
    x_status  := 'Success';

EXCEPTION
    WHEN OTHERS THEN
        dbms_output.put_line('Failure');
        x_status  := 'Failure';           

END main;
END PKG_SP_EMPLOYEE;

JDBC代码

Connection con= null;
OracleCallableStatement cstmt = null;

try {

    Class.forName("oracle.jdbc.driver.OracleDriver");
    con=DriverManager.getConnection("jdbc:oracle:thin:@test.com:1521:TESTAD",
                    "user","password");

    cstmt = (OracleCallableStatement)con.prepareCall (
                "begin PKG_SP_EMPLOYEE.MAIN (?,?); end;"); 

    String[] values = { "1", "John Doe", "HR" };

    cstmt.setPlsqlIndexTable (1, values, values.length, values.length, OracleTypes.VARCHAR, 3);

    cstmt.registerOutParameter(2, java.sql.Types.VARCHAR); 

    cstmt.execute ();

//...

Java异常

java.sql.SQLException: ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'MAIN'
ORA-06550: line 1, column 43:
**PLS-00418: array bind type must match PL/SQL table row type**
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored

查看Oracle documentation here之后,我看到Oracle JDBC驱动程序支持VARCHAR和NUMBER类型的PL / SQL关联数组。这是否意味着不支持我的代码中所示的复杂类型的关联数组?

0 个答案:

没有答案