我从java调用存储过程。哪个返回参数是对象类型。
对象类型:
create or replace TYPE xx_hr_leave_record as OBJECT
(
employee_name varchar2 (250),
employee_number varchar2(10),
Organization varchar2(250)
);
程序:
PROCEDURE GetLeaveInfo(p_notification_id IN NUMBER,
p_leave_detail OUT NOCOPY xx_hr_leave_record,
p_result OUT NOCOPY NUMBER,
p_error_code OUT NOCOPY NUMBER,
p_error_msg OUT NOCOPY VARCHAR2);
我使用pl / sql调用此过程,返回正确的输出。
但是我在以下链接的帮助下调用上述程序
http://betteratoracle.com/posts/31-passing-record-types-between-oracle-and-java
我的Java代码:
package com.ind.middleware;
import java.sql.Array;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.Struct;
import java.sql.Types;
import java.util.ArrayList;
import oracle.jdbc.driver.OracleTypes;
import oracle.sql.StructDescriptor;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import com.db.DBConnectionManager;
public class AbsenceDetail
{
int notificationId;
Connection conn =null;
ResultSet rset = null;
Statement stmt = null;
CallableStatement cstmt=null;
ArrayList<String> aryNotificationType = null;
DBConnectionManager db;
String qry="";
String NotificationType=null;
final String typeName = "xx_hr_leave_record";
JSONArray json = new JSONArray();
int result,error_code;
String error_msg= "";
@SuppressWarnings("unchecked")
public void getData() throws Exception
{
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 APPS.EVO_HR_LEAVE_SERVICES.GetLeaveInfo(?, ?, ?, ?, ?)}");
cstmt.setInt(1,254163);
cstmt.registerOutParameter(2, OracleTypes.STRUCT,typeName.toUpperCase());
cstmt.registerOutParameter(3,Types.INTEGER);
cstmt.registerOutParameter(4,Types.INTEGER);
cstmt.registerOutParameter(5,Types.CHAR);
cstmt.execute();
if(cstmt.getInt(3) == 0)
{
Object[] data = (Object[]) ((Array) cstmt.getObject(2)).getArray();
System.out.print("data length : " + data.length);
for(Object tmpAbsence : data)
{
JSONObject jObj = new JSONObject();
Struct rowAbsence = (Struct) tmpAbsence;
int j = 1;
for(Object attribute : rowAbsence.getAttributes())
{
System.out.println(metaData.getColumnName(j) + " = " + attribute);
if(metaData.getColumnName(j).equals("employee_name"))
{
jObj.put(metaData.getColumnName(j), attribute);
}
if(metaData.getColumnName(j).equals("employee_number"))
{
jObj.put(metaData.getColumnName(j), attribute);
}
else if(metaData.getColumnName(j).equals("Organization"))
{
jObj.put(metaData.getColumnName(j), attribute);
}
j++;
}
}
}
}
}
catch(Exception e)
{
System.out.println("ERROR in : ");
e.printStackTrace();
}
}
public static void main(String []arg)
{
AbsenceDetail wa = new AbsenceDetail();
try {
System.out.println("Absence Detail class");
wa.getData();
} catch (Exception e) {
e.printStackTrace();
}
}
}