简体   繁体   English

如何从PLSQL中将表用户定义的类型作为out param?

[英]How do I get table user defined types from PLSQL as out param?

I have a PLSQL code with the following signature. 我有一个带有以下签名的PLSQL代码。

procedure getall(
   p_id               in number,
   p_code             in varchar2,
   x_result           out tt_objs);

type rt_obj is record(
    val1              mytable.attr1%type
    val2              mytable.attr2%type
    val3              mytable.attr2%type
);

type tt_objs is table of rt_obj index by binary_integer;

What should be the Java code that can invoke this procedure and read x_result ? 应该调用此过程并读取x_result的Java代码应该是什么?

Maybe this could be what you are looking for. 也许可能是你想要的。

This should be the interesting part: 这应该是有趣的部分:

//oracle.sql.ARRAY we will use as out parameter from the package
//and will store pl/sql table
ARRAY message_display = null; 

//ArrayList to store object of type struct 
ArrayList arow= new ArrayList();

//StructDescriptor >> use to describe pl/sql object
//type in java.
StructDescriptor voRowStruct = null; 

//ArrayDescriptor >> Use to describe pl/sql table
//as Array of objects in java
ArrayDescriptor arrydesc = null;

//Input array to pl/sql procedure
ARRAY p_message_list = null;

//Oracle callable statement used to execute procedure
OracleCallableStatement cStmt=null;

try
{
//initializing object types in java.
voRowStruct = StructDescriptor.createDescriptor("RECTYPE",conn); 
arrydesc = ArrayDescriptor.createDescriptor("RECTAB",conn);
}

catch (Exception e)
{
throw OAException.wrapperException(e);
}

for(XXVORowImpl row = (XXVORowImpl)XXVO.first(); 
row!=null;
row = (XXVORowImpl)XXVO.next())
{
//We have made this method to create struct arraylist
// from which we will make ARRAY
//the reason being in java ARRAY length cannot be dynamic
//see the method defination below.
populateObjectArraylist(row,voRowStruct,arow); 
}

//make array from arraylist
STRUCT [] obRows= new STRUCT[arow.size()];
for(int i=0;i < arow.size();i++)
{
obRows[i]=(STRUCT)arow.get(i);
}

try 
{
p_message_list = new ARRAY(arrydesc,conn,obRows); 
}
catch (Exception e)
{
throw OAException.wrapperException(e);
}

//jdbc code to execute pl/sql procedure 
try
{
cStmt
=(OracleCallableStatement)conn.prepareCall("{CALL ioStructArray.testproc(:1,:2)}"); 
cStmt.setArray(1,p_message_list);
cStmt.registerOutParameter(2,OracleTypes.ARRAY,"RECTAB"); 
cStmt.execute();

//getting Array back
message_display = cStmt.getARRAY(2);
//Getting sql data types in oracle.sql.datum array
//which will typecast the object types
Datum[] arrMessage = message_display.getOracleArray();

//getting data and printing it
for (int i = 0; i < arrMessage.length; i++)
{ 
oracle.sql.STRUCT os = (oracle.sql.STRUCT)arrMessage[i];
Object[] a = os.getAttributes(); 
System.out.println("a [0 ] >>attribute1=" + a[0]); 
System.out.println("a [1 ] >>attribute2=" + a[1]);
System.out.println("a [2 ] >>attribute3=" + a[2]);

Yep, it's not possible directly. 是的,这是不可能直接的。 You can either 你也可以

  • Create a public type with the same structure as the PLSQL record and follow the Eggi's advice. 创建一个与PLSQL记录结构相同的公共类型,并遵循Eggi的建议。 Similar approach uses Oracle JPublisher . 类似的方法使用Oracle JPublisher JPublisher can help you to automate this process. JPublisher可以帮助您自动执行此过程。
  • Or you can use anonymous PLSQL block to create or read PLSQL records. 或者您可以使用匿名PLSQL块来创建或读取PLSQL记录。 We are thinking about creating a library do it automatically in our company. 我们正在考虑创建一个库,在我们公司自动执行。
  • Or you can create a wrapper functions to wrap records to XML (both in Java and PLSQL). 或者,您可以创建一个包装函数来将记录包装到XML(Java和PLSQL中)。 Then pass XML as Xmltype or CLOB between DB and Java. 然后将XML作为Xmltype或CLOB在DB和Java之间传递。 We have already this solution for some complicated structures. 对于一些复杂的结构,我们已经有了这个解 It's tedious and slows down a processing a little bit, but it works. 它很繁琐,并且稍微减慢了处理速度,但它确实有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何从用户输入的ArrayList中获取一个值? - How do I get an out a value from an ArrayList from the user typing it in? 如何获得用户定义的多个注释,这些注释具有构造函数中预期的参数类型? - How can I get multiple annotations defined by a user, that have the types of parameters expected in a constructor? 如何从springboot调用用户定义的sql function? - How do I call a user defined sql function from springboot? 如何让 Scanner 从用户输入中挑选出关键字? - How do I get Scanner to pick out a keyword from user input? 用户输入特定字符串时,如何跳出循环? - How do i break out of a loop when the user types in a specific string? 如何格式化从用户获得的日期作为输入,然后在日历中找到该日期以找出该日历中的day_of_month? - how do i format date that i get from user as input and then find that date in a calendar to find out which day_of_month is it in that calendar? 如何解决这些不兼容的类型? - How do I get around these incompatible types? 测试时如何从 MockWebServer 中提取参数? - How do I extract param from MockWebServer when testing? 我如何从freemarker设置struts param标签的值 - How do I set the value of a struts param tag from freemarker 我如何将我的方法中的 output 信息作为一个表发送给用户? - How do I output information from my Method to the user as a table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM