[英]How to get array of object type parse in java from oracle procedure
我有一個Oracle過程返回兩個參數,第一個是數組對象類型,第二個是數字。
我的程序是:
CREATE OR REPLACE PROCEDURE APPS.xx_push_notification (
p_user_name IN VARCHAR2,
p_hr_type IN VARCHAR2,
p_ret_array OUT NOCOPY XX_WF_SVC_NTF_ARRAY,
p_ret_array_size OUT NOCOPY NUMBER
)
AS
--l_item_type IN VARCHAR2,l_message_names IN VARCHAR2,
l_orig_system VARCHAR2 (4000);
l_orig_system_id NUMBER;
l_ret_array xx_wf_svc_ntf_array;
l_ret_array_size NUMBER;
l_item_type VARCHAR2 (1000);
l_message_names VARCHAR2 (4000);
l_latest_ntf_id NUMBER;
CURSOR cur_hrms (
cur_orig_system VARCHAR2,
cur_orig_system_id NUMBER
)
IS
......................
......................
......................
BEGIN
......................
......................
......................
p_ret_array := xx_wf_svc_ntf_array ();
IF p_hr_type = 'HR_ABSENCES'
THEN
FOR c1_rec IN cur_hrms (l_orig_system, l_orig_system_id)
LOOP
p_ret_array.EXTEND;
p_ret_array (p_ret_array.COUNT) :=
xx_wf_svc_ntf_record (c1_rec.NOTIFICATION_ID,
c1_rec.CONTEXT,
c1_rec.FROM_USER,
c1_rec.TO_USER,
c1_rec.SUBTYPE);
END LOOP;
p_ret_array_size := p_ret_array.COUNT;
END IF;
END;
/
oracle類型對象:
create or replace type xx_wf_svc_ntf_record is object (NOTIFICATION_ID NUMBER,
CONTEXT VARCHAR2(2000),
FROM_USER VARCHAR2(320),
TO_USER VARCHAR2(320),
SUBJECT VARCHAR2(2000),
SUBTYPE VARCHAR2(32));
/
oracle類型數組:
create or replace type xx_wf_svc_ntf_array is table of xx_wf_svc_ntf_record ;
/
我的Java代碼是:
.......
.......
db = new DBConnectionManager();
conn=db.getConnection();
if(conn!=null)
{
cstmt = conn.prepareCall("{call xx_push_notification(?, ?, ?, ?)}");
cstmt.setString(1, UserName);
cstmt.setString(2, NotificationType);
cstmt.registerOutParameter(3, OracleTypes.ARRAY,typeTableName);
cstmt.registerOutParameter(4, Types.INTEGER);
cstmt.execute();
int newRecord=cstmt.getInt(4);
System.out.println("Total New Record : "+newRecord);
}
.........
.........
我得到第二個參數,這是返回數組的大小。 我有很多用於解析oracle類型數組的研發。 我得到了由oracle過程返回的單數組解析。 但是我不知道如何解析oracle類型的數組對象。
提前致謝。
作為輸出參數,如果您嘗試使用Oracle類型和類,則您的代碼可能會受到使用的任何連接池的干擾。 我建議您將其閱讀java.sql.Array
實例構成的普通java.sql.Struct
。
也許您可以通過執行以下操作使其工作:
java.sql.Struct records[] =
(java.sql.Struct[]) ((java.sql.Array) cstmt.getObject(3)).getArray()
然后,您可以簡單地獲取該數組中每個Struct
的屬性。
我找到了我的問題的答案。
package Mobile.test;
import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Struct;
import java.sql.Types;
import java.util.ArrayList;
import oracle.sql.StructDescriptor;
import com.db.DBConnectionManager;
import com.test.PushNotification;
public class SentNotification implements Runnable {
Connection conn =null;
ResultSet rset = null;
Statement stmt = null;
CallableStatement cstmt=null;
ArrayList<String> aryNotificationType = null;
DBConnectionManager db;
String UserName=null;
String NotificationType=null;
final String typeName = "xx_test";
final String typeTableName = "xx_test_table";
public SentNotification(String UserName,String NotificationType)
{
this.UserName=UserName;
this.NotificationType=NotificationType;
}
@Override
public void run() {
// TODO Auto-generated method stub
try
{
db = new DBConnectionManager();
conn=db.getConnection();
if(conn!=null)
{
final StructDescriptor structDescriptor = StructDescriptor.createDescriptor(typeName.toUpperCase(), conn);
final ResultSetMetaData metaData = structDescriptor.getMetaData();
cstmt = conn.prepareCall("{call xx_push_test(?, ?, ?, ?)}");
cstmt.setString(1, UserName);
cstmt.setString(2, NotificationType);
cstmt.registerOutParameter(3, Types.ARRAY, typeTableName.toUpperCase());
cstmt.registerOutParameter(4, Types.INTEGER);
cstmt.execute();
int newRecord=cstmt.getInt(4);
System.out.println("Total New Record : "+newRecord);
if(newRecord > 0)
{
Object[] data = (Object[]) ((Array) cstmt.getObject(3)).getArray();
for(Object tmp : data)
{
Struct row = (Struct) tmp;
int i = 1;
for(Object attribute : row.getAttributes())
{
if(metaData.getColumnName(i).equals("NOTIFICATION_ID"))
System.out.println(metaData.getColumnName(i) + " = " + attribute);
++i;
}
}
PushNotification pn = new PushNotification();
pn.sendPushNotification(UserName,""+newRecord,NotificationType);
System.out.println(UserName+" you have " + newRecord+" "+NotificationType);
}
}
else
{
System.out.println("Connecation is Null");
}
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try
{
cstmt.close();
conn.close();
}
catch (SQLException e)
{
e.printStackTrace();
System.out.println("issue"+e);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.