简体   繁体   English

Java:将数组发送到PL-SQL函数

[英]Java: Send array to a PL-SQL function

I need to pass an array of objects to a PL-SQL function 我需要将一个对象数组传递给PL-SQL函数

My ORACLE code: 我的ORACLE代码:

CREATE OR REPLACE
TYPE
        uu.ITEMTAB AS TABLE OF ITEMREC; 
/

CREATE OR REPLACE
TYPE
        uu.ITEMREC AS OBJECT (ID NUMBER,
                               NAME VARCHAR2(30)) 
/

Java class Java类

public class ITEMREC {

    private int ID;
    private String NAME;

    public ITEMREC(int iD, String nAME) {
        super();
        ID = iD;
        NAME = nAME;
    }
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public String getNAME() {
        return NAME;
    }
    public void setNAME(String nAME) {
        NAME = nAME;
    }
}

Java code: Java代码:

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;

import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;

public class Testna {

    public static void main(String args[]) throws java.io.IOException, SQLException, ClassNotFoundException
    {

        ITEMREC[] myA = new ITEMREC[1];
        myA[0] = new ITEMREC(1, "BOB");

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","uu","uu");

        ArrayDescriptor desc= ArrayDescriptor.createDescriptor("ITEMTAB", con);  
        ARRAY array = new ARRAY(desc, con, myA);

        System.out.println("Connection created..............");
        String call = "{ ? = call mainpackage.fgetText(?) }";
        CallableStatement cstmt = con.prepareCall(call);
        cstmt.setQueryTimeout(1800);
        cstmt.registerOutParameter(1, Types.VARCHAR);
        cstmt.setArray(2, array);
        cstmt.executeUpdate();
        String val = cstmt.getString(1);
        cstmt.close();
        con.close();
        System.out.println(val);
    }   
}

Note: here I have only one object, as this is just for test. 注意:这里我只有一个对象,因为这只是为了测试。

The new error I get is at: 我得到的新错误是:

ARRAY array = new ARRAY(desc, con, myA);

Exception in thread "main" java.sql.SQLException: Fail to convert to internal representation:

UPDATED: I have updated my code, 更新:我更新了我的代码,

Thanx @Codo Thanx @Codo

It's slightly more complicated than that. 它稍微复杂一些。 First, you need an array type in Oracle. 首先,您需要Oracle中的数组类型。 It must be global, ie not defined with an PL/SQL package. 它必须是全局的,即不使用PL / SQL包定义。

create or replace type NUM_ARRAY as table of number;

Then you need to create an Oracle specific array in Java: 然后,您需要在Java中创建Oracle特定的数组:

int intArray[] = { 1,2,3,4,5,6 };

ArrayDescriptor desc= ArrayDescriptor.createDescriptor("NUM_ARRAY", con);  
ARRAY array = new ARRAY(desc, con, intArray);

This can now be passed to the stored procedure: 现在可以将其传递给存储过程:

OraclePreparedStatement stmt =
  (OraclePreparedStatement)conn.prepareStatement("begin pkg.proc(:x); end;");
ps.setARRAY( 1, array_to_pass );  
ps.execute();

I'm not quite sure how important it is to use the Oracle specific classes from the oracle.jdbc package. 我不太确定使用oracle.jdbc包中的Oracle特定类有多重要。 But it's certainly not possible to work with pure JDBC. 但是使用纯JDBC肯定是不可能的。

If you provide more information about your array type (both on the Java and the Oracle side) as well as the signature of the PL/SQL procedure, I could give you more specific advice. 如果您提供有关阵列类型的更多信息(包括Java和Oracle端)以及PL / SQL过程的签名,我可以为您提供更具体的建议。

Updated: 更新:

Obviously, you're not trying to pass an array but a table. 显然,你不是试图传递一个数组而是一个表。 I've never done that and I don't know whether it's possible or not. 我从来没有这样做,我不知道是否可能。

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

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