---
title: "【转】在java中处理oracle中的对象数组（嵌套表） "
author: "fspwp"
date: "2011-08-30T16:49:46Z"
tags: []
categories: ["数据库"]
slug: "java-oracle"
draft: false
lastmod: "2011-08-30T16:49:46Z"
id: 1161615
url: "https://fspwp.iteye.com/blog/1161615"
---

在java中处理oracle中的对象数组（嵌套表）：

sp文件：

------------------------------- Create Object Type ----------------------------------  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) create   or   replace  type o\_tmp  as  object (  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) userid  varchar2 ( 10 ),  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) username  varchar2 ( 20 )  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) /  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) --------------------------- Create Nested Table Type  On Base Object Type------------------------  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) create   or   replace  type tmp\_array  is   table   of  o\_tmp；  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) /  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
　---------------------------- Create a Package File -----------------------------------------------------------  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) create   or   replace  package body test\_object  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) as  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) procedure  getObject(obj\_arr out tmp\_array)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) is  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) tmp\_obj o\_tmp;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) test\_arr tmp\_array : =  tmp\_array();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) n  number  : =   1 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) begin  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) loop  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) exit   when  n  &gt;   5 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) tmp\_obj : =  o\_tmp( ' test1 '   ||  n,  ' fuyue '   ||  n);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) test\_arr.extend;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) test\_arr(n) : =  tmp\_obj;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) n : =  n  +   1 ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) end  loop;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) obj\_arr : =  test\_arr;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) for  n  in  obj\_arr.first .. obj\_arr.last loop  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) tmp\_obj : =  obj\_arr(n);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) dbms\_output.put\_line(tmp\_obj.userid  ||   '  ---  '   ||  tmp\_obj.username);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) end  loop;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) end  getObject;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) end  test\_object;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) /

 

java文件：

![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)import  java.sql.Array;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  java.sql.CallableStatement;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  java.sql.DriverManager;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  java.sql.SQLException;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  oracle.jdbc.OracleConnection;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  oracle.jdbc.driver.OracleTypes;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  oracle.sql.Datum;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif) import  oracle.sql.STRUCT;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif) public   class  ObjArray  {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private   static  String strDriver  =   " oracle.jdbc.driver.OracleDriver " ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      private   static  String strUrl  =   " jdbc:oracle:thin:@192.160.10.14:1521:avex " ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)      /\**  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)      *  @param  args  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)       \*/  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)      public   static   void  main(String\[] args)  {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)          //  TODO Auto-generated method stub  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)         test();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)      private   static   void  test()  {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)          try   {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             Class.forName(strDriver);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             OracleConnection con  =  (OracleConnection) DriverManager  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                     .getConnection(strUrl,  " avex " ,  " avex " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             CallableStatement stmt  =   null ;  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             stmt  =  con.prepareCall( " {call test\_object.getObject(?)} " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             stmt.registerOutParameter( 1 , OracleTypes.ARRAY,  " TMP\_ARRAY " );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             stmt.execute();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             Array outparam  =  stmt.getArray( 1 );  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             Object\[] obj  =  (Object\[]) outparam.getArray();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)              for  ( int  i  =   0 ; i  &lt;  obj.length; i ++ )  {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                 STRUCT struct  =  (STRUCT) obj\[i];  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                 Datum\[] dt  =  struct.getOracleAttributes();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)                  for  ( int  n  =   0 ; n  &lt;  dt.length; n ++ )  {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)                     System.out.println(dt\[n]);  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)                 }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)             }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)         }   catch  (ClassNotFoundException e)  {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              //  TODO Auto-generated catch block  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)         }   catch  (SQLException e)  {  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)              //  TODO Auto-generated catch block  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)             e.printStackTrace();  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)         }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)     }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif) }  
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)

 

转自：http://blog.csdn.net/hunterdiary/article/details/1484767