This answer on another forum may help you.
Look at the sample at the bottom of the post to find out how to choose from the collection returned by the Java method (it can also be a Java stored procedure).
Here is an example of how to do this with a Java stored procedure
1) Create a DB object to determine the type of returned rows:
create type try_obj as object ( field_a number, field_b varchar2(10) ) / create type try_obj_tab as table of try_obj /
2) Create a Java class in the database with a static method (GetSampleResult) that returns a collection
create or replace and compile java source named QueryReturn as import java.sql.*; import java.util.*; import oracle.jdbc.*; import oracle.jdbc.pool.OracleDataSource; import oracle.sql.*; public class QueryReturn implements ORADataFactory,ORAData{ private NUMBER field1; private CHAR field2; public QueryReturn(OracleConnection conn,int n,String c) throws SQLException { field1 = new NUMBER(n); field2 = new CHAR(c,oracle.sql.CharacterSet.make(conn.getStructAttrCsId())); } public QueryReturn(NUMBER n, CHAR c) { field1 = n; field2 = c; } public QueryReturn(Object[] attributes) { this( (NUMBER) attributes[0], (CHAR) attributes[1] ); } public QueryReturn(Datum d) throws SQLException { this(((STRUCT) d).getOracleAttributes()); } public ORAData create(Datum d, int sqlType) throws SQLException { if (d == null) return null; else { return new QueryReturn(d); } } public STRUCT toSTRUCT(Connection conn) throws SQLException { StructDescriptor sd = StructDescriptor.createDescriptor("TRY_OBJ", conn); Object [] attributes = { field1,field2 }; return new STRUCT(sd, conn, attributes); } public Datum toDatum(Connection conn) throws SQLException { return toSTRUCT(conn); } public static ARRAY GetSampleResult() throws SQLException, ClassNotFoundException {
3) Create a Wrap to use the Java stored procedure in a function
create or replace function GetSampleResult return try_obj_tab AS LANGUAGE JAVA NAME 'QueryReturn.GetSampleResult() return oracle.sql.ARRAY';
4) Show result
SQL> select * 2 from table(GetSampleResult()) 3 / FIELD_A FIELD_B ---------- ---------- 1 abc 2 dce SQL>
source share