1

I am trying to pass array of values from java to PL/SQL function. I am trying as below,

String[] array = {"70" , "2" , "4" , "9" , "329" , "13" , "49" , "33"};
Connection cnn=DriverManager.getConnection(url, username, password);
 ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("VLIST", cnn);
ARRAY array_to_pass = new ARRAY(descriptor, cnn,array);
 OracleCallableStatement pstmt = (OracleCallableStatement) cnn.prepareCall("{? = call  GETSRCHLST2(?)}");
pstmt.registerOutParameter(1, Types.VARCHAR);
 pstmt.setArray(2, array_to_pass);
pstmt.execute();
String output = pstmt.getString(1);
out.println(output+"\n"+ " "+array_to_pass.length());

Here in the function call no content of array is getting passed but array_to_pass.length() is 8.

Oracle procedure:

CREATE OR REPLACE FUNCTION EDR.GETSRCHLST2(VV VLIST) RETURN VARCHAR2 IS

BN NUMBER;
STRS VARCHAR2(9000):='start';
HH varchar2(30);

BEGIN

STRS:=STRS || '- LIST COUNT: '||VV.COUNT||' - ';
BN:=VV.FIRST;

WHILE BN IS NOT NULL
LOOP

   HH:=VV(BN);
   STRS:=STRS||HH||' , ';
   BN := VV.NEXT(BN);   

END LOOP;

RETURN STRS;

EXCEPTION WHEN OTHERS THEN RETURN 'SOME ERROR'||SQLERRM;
end;

Can anyone tell me, where i am going wrong?

techhunter
  • 683
  • 1
  • 12
  • 27

2 Answers2

0

Hope it may help those who is in search of same solution.

CREATE OR REPLACE
TYPE DR.VLIST AS TABLE OF VARCHAR2(50)

But the value passed from java in array will not match VARCHAR instead it expects NVARCHAR. So made a slight changes as ,

CREATE OR REPLACE
TYPE DR.VLIST AS TABLE OF NVARCHAR2(50)

it solved my the problem.

techhunter
  • 683
  • 1
  • 12
  • 27
0

oracle.sql.ARRAY is deprecated. Instead, you can call OracleConnection.createOracleArray(String arrayTypeName, Object elements)
Note that arrayTypeName should be the table of type and elements should be your Java array T[] where T corresponds to the type that arrayTypeName is a list of. (this can be a class implementing SQLData)

For more information, you might find my my answer on getArray() and writeArray() useful.

Arlo
  • 1,331
  • 2
  • 15
  • 26