1

i want to access a data type defined in a package from java when executing function.Please help me.

Package and function:

create or replace package types as 
    TYPE name_array IS VARRAY(100000) OF VARCHAR2(200);
end types;



create or replace function test_pack1 return types.name_array as 
names types.name_array := types.name_array(); 

begin

for emp in (Select state from test where test_ID BETWEEN 1 AND 120000) loop
 names.extend;
 names(names.count) := emp.state;
  end loop;
return names;
end test_pack1;

java code

cstmt = (OracleCallableStatement) con.prepareCall("begin ? :=test_pack1; end;");
cstmt.registerOutParameter(1, OracleTypes.ARRAY,"NAME_ARRAY");
cstmt.execute();

In the above i am getting error at the second line. Err msg below: java.sql.SQLException: invalid name pattern: xyz.NAME_ARRAY

function is getting executed in Oracle successfully.But when we try execute from java its throwing above exception.

3
  • I would not consider myself an expert on this topic, but: What happens if you use ARRAY instead of NAME_ARRAY? Commented Jan 3, 2012 at 11:40
  • I'm not an expert either, but aren't you missing package name in your java code - like: cstmt.registerOutParameter(1, OracleTypes.ARRAY,"types.NAME_ARRAY"); ? Commented Jan 3, 2012 at 12:42
  • @davida. i tried this but its not working... Commented Jan 4, 2012 at 7:27

3 Answers 3

2

Newer versions of Oracle (since 12c Release 1) claim to support types inside of packages (so they can be accessed via JDBC).

All PL/SQL package types are mapped to a system-wide unique name that can be used by JDBC to retrieve the server-side type metadata. The name is in the following form:

[SCHEMA.]<PACKAGE>.<TYPE>

from here: http://docs.oracle.com/cd/E16655_01/java.121/e17657/apxref.htm#CHEIIJCC

Sign up to request clarification or add additional context in comments.

Comments

1

You have to use SQL type for this purpose, not PL/SQL type, so you need to define the type outside of PL/SQL (i.e. on schema level, not package level):

CREATE OR REPLACE TYPE  NAME_ARRAY AS VARRAY(100000) OF VARCHAR2(200);

and use it in your function

create or replace function test_pack1 return name_array as 
   names name_array := name_array(); 
...

2 Comments

i don have privelage to create type @ schema level .please suggest any other ways..
Try to use some existing type, for example in 10g there is DBMSOUTPUT_LINESARRAY which is VARRAY(2147483647) OF VARCHAR2(32767)
0

For this to work it is not possible for the array type to be defined in a package, as much as it is convenient to do so. Oracle JDBC driver is not able to "see" package types in this manner (this applies not only to arrays, but to all custom defined types you may want to return via a JDBC call).

You need to define the array as a schema level type instead, after which this sort of code should be able to work fine.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.