Use of table variables in Oracle in JDBC

Source: Internet
Author: User

1. First define a method to return the array type
Create or replace type my_table_type is table of varchar2 (20 );
 
Create or replace function func
Return my_table_type
Is
I my_table_type: = my_table_type ();
Begin
Select name bulk collect into I from emps;
Return I;
End;
 
2. Call in JDBC. If the returned table variable is
 
Java code
Public void SelectAgus (String SQL)
{
OracleCallableStatement call = null;
Try
{
Call = (OracleCallableStatement) con. prepareCall (SQL );
// If table is returned, ARRAY is used. If the returned OBJECT is returned, STRUCT is used.
// The third parameter defines the table type name.
Call. registerOutParameter (1, OracleTypes. ARRAY, "MY_TABLE_TYPE ");
Call.exe cute ();
// Obtain the first parameter (return value)
ARRAY array = call. getARRAY (1 );
// Obtain the elements in the table
Datum [] dat = array. getOracleArray ();
// Print the traversal in sequence
For (Datum d: dat)
{
System. out. println (new String (d. getBytes ()));
}
} Catch (Exception e)
{
E. printStackTrace ();
}
}
2. If the nested table structure is defined,
Definition:
Create or replace type all_table is object (id number, name varchar2 (20 ));
Create or replace type emps_table_type is table of all_table;
-- Create a function. The return type is emps_table_type;
Create or replace function funcc
Return emps_table_type
Is
I emps_table_type;
Begin
-- Read all the values of ID and NAME in emps to I.
Select all_table (id, name) bulk collect into I from emps;
Return I; -- return table
End;



Java code
Public void SelectAgus (String SQL)
{
OracleCallableStatement call = null;
Try
{
Call = (OracleCallableStatement) con. prepareCall (SQL );
Call. registerOutParameter (1, OracleTypes. ARRAY, "EMPS_TABLE_TYPE ");
Call.exe cute ();
ARRAY array = call. getARRAY (1 );
Datum [] dat = array. getOracleArray ();
For (Datum d: dat)
{// Obtain the elements in a row
STRUCT struct = (STRUCT) d;
// Garbled characters may occur here, so I use two methods to obtain elements respectively.
Datum [] d1 = struct. getOracleAttributes ();
Object [] d2 = struct. getAttributes ();
System. out. println ("ID =" + d2 [0] + "" + "NAME =" +
New String (d1 [1]. getBytes ()));
}

} Catch (Exception e ){
E. printStackTrace ();
}
}

 

Author "aazham"
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.