---
title: "jdbc从存储过程返回单个对象或PL/SQL表"
author: "springking"
date: "2010-03-15T18:04:46Z"
tags: []
categories: ["数据库"]
slug: "jdbc-pl-sql"
draft: false
lastmod: "2010-03-15T18:45:01Z"
id: 616338
url: "https://springking.iteye.com/blog/616338"
---

返回单个PL/SQL表，元素为标量类型  
创建类型，包头，包体  
create or replace type tab\_array is table of varchar2(38);  
/

create or replace package addnum  
is  
  procedure abc(e\_name in tab\_array,t\_name out tab\_array);  
end;  
/

create or replace package body addnum is  
  procedure abc  
  (e\_name in tab\_array,t\_name out tab\_array)  
  is  
  begin  
      for i in 1..e\_name.count loop  
          insert into t(name) values(e\_name(i));  
      end loop;      
       
      select name bulk collect into t\_name from t;  
  end;  
end;  
/

JDBC操作  
package com.lovo;

import java.sql.Connection;  
import java.sql.DriverManager;

import oracle.jdbc.OracleCallableStatement;  
import oracle.jdbc.OracleTypes;  
import oracle.sql.ARRAY;  
import oracle.sql.ArrayDescriptor;  
import oracle.sql.Datum;

public class JDBCMain {  
    public static void main(String\[] args) throws Exception {  
        new oracle.jdbc.driver.OracleDriver();  
         
        Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@192.168.0.99:1521:HXM", "me", "123");  
        String\[] strs = new String\[]{"abc","cba"};  
        OracleCallableStatement  call = (OracleCallableStatement) conn.prepareCall("{call addnum.abc(?,?)}");  
        ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("TAB\_ARRAY", conn); /\*TAB\_ARRAY是类型名，必须大写\*/  
        call.setArray(1, new ARRAY(descriptor,conn,strs));  
        call.registerOutParameter(2, OracleTypes.ARRAY,"TAB\_ARRAY");  
        call.execute();          
        ARRAY arr = call.getARRAY(2);  
        Datum\[] dat = arr.getOracleArray();  
        for(int i=0;i&lt;dat.length;i++){  
            System.out.println(dat\[i]);  
        }  
         
        conn.close();  
    }  
}

返回单个对象  
    建立对象类型  
        create or replace type t\_obj as object  
        (id int,  
         name varchar2(20)  
        );  
         
    建立对象表  
        create table t of t\_obj;  
         
        insert into t values(t\_obj(1,'abc1'));  
        insert into t values(t\_obj(2,'abc2'));  
        insert into t values(t\_obj(3,'abc3'));  
        insert into t values(t\_obj(4,'abc4'));  
        insert into t values(t\_obj(5,'abc5'));  
         
    建立包头和包体  
    create or replace package addnum  
    is  
      procedure abc(e\_name in int,t\_per out t\_obj);  
    end;  
    /  
     
    create or replace package body addnum is  
      procedure abc  
      (e\_name in int,t\_per out t\_obj)  
      is  
      begin  
           select value(p) into t\_per from t p where p.id=e\_name;  
      end;  
    end;  
    /

jdbc操作  
    TypeDescriptor td = TypeDescriptor.getTypeDescriptor("T\_OBJ", (OracleConnection) conn);  
        OracleCallableStatement call = (OracleCallableStatement) conn.prepareCall("{call addnum.abc(?,?)}");  
        call.setInt(1, 3);  
        call.registerOutParameter(2, OracleTypes.STRUCT, "T\_OBJ");  
        call.execute();  
         
        STRUCT str = call.getSTRUCT(2);  
         
        Object\[] objs = str.getAttributes();  
        for(int i=0;i&lt;objs.length;i++){  
            System.out.println(str.getDescriptor().getMetaData().getColumnName(i+1) + "," + objs\[i]);  
        }  
         
返回1个表，元素是对象  
    建立对象类型，表类型，包头，包体  
create or replace type t\_obj as object  
(id int,  
 name varchar2(20)  
);  
/  
create or replace type t\_table is table of t\_obj;  
/

create or replace package addnum  
is  
  procedure abc(e\_name in int,t\_per out t\_table);  
end;  
/

create or replace package body addnum is  
  procedure abc  
  (e\_name in int,t\_per out t\_table)  
  is  
  begin  
       select value(p) bulk collect into t\_per from t p;  
  end;  
end;  
/  
jdbc操作  
        TypeDescriptor td = TypeDescriptor.getTypeDescriptor("T\_TABLE", (OracleConnection) conn);  
        OracleCallableStatement call = (OracleCallableStatement) conn.prepareCall("{call addnum.abc(?,?)}");  
        call.setInt(1, 1);  
        call.registerOutParameter(2, OracleTypes.ARRAY,"T\_TABLE");  
        call.execute();  
         
        ARRAY arr = call.getARRAY(2);  
        Datum\[] dat = arr.getOracleArray();  
         
        for(int i=0;i&lt;dat.length;i++){  
            STRUCT struct = (STRUCT) dat\[i];  
             
            Object\[] objs = struct.getAttributes();  
            System.out.println("id:" + objs\[0] + ",name:" + objs\[1]);  
        }

 

在oracle 10g上测试通过