如何从java类调用带有out参数的过程作为表类型(2)

时间:2013-05-31 06:47:48

标签: java oracle plsql oracle11g

答案接缝已经在这里得到解答: how to call procedure with out parameter as table type from a java class

但我们有问题,返回值vor代码总是" ???&#34 ;,正确返回trxSeq的值。 如果我们直接在db上调用它,代码就会被填充值。

我们尝试将代码类型为varchar2,char,varchar。没有区别。

desc t_mam_code:
       TYPE t_mam_code AS OBJECT(                              
         code VARCHAR2(30),                                                
         trxSeq NUMBER(12)

java方法

final String typeName = "T_MAM_CODE";
final String typeTableName = "T_MAM_CODE_TAB";

// no difference, if we use Oracle Connection or java.sql.Connection
OracleConnection oracleConnection= connection.unwrap(OracleConnection.class);

// Get a description of your type (Oracle specific)
final StructDescriptor structDescriptor = StructDescriptor.createDescriptor(typeName, oracleConnection);
final ResultSetMetaData metaData = structDescriptor.getMetaData();

CallableStatement call = oracleConnection.prepareCall("{call business.getCodes(?, ?, ?, ?, ?, ?, ?)}");
// CallableStatement call = connection.prepareCall("{call business.getCodes(?, ?, ?, ?, ?, ?, ?)}");

int i = 1;
call.setString(i++, shopId);
call.setDate(i++, new java.sql.Date(consumerStamp.getTime()));
call.setInt(i++, version);
...

int out1 = i++;

call.registerOutParameter(out1, Types.ARRAY, typeTableName);
//call.registerOutParameter(out1, OracleTypes.ARRAY, typeTableName);

call.execute();

Object[] data = (Object[]) ((Array) call.getObject(out1)).getArray();
for(Object tmp : data) {
  Struct row = (Struct) tmp;
  // Attributes are index 1 based...
  int idx = 1;
  for(Object attribute : row.getAttributes()) {               
     System.out.println(metaData.getColumnName(idx) + " " + attribute);                                            
     ++idx;
}

输出是:

CODE ??? 
TRXSEQ 200001520606 ...

输出应为:

CODE ABC1234 
TRXSEQ 200001520606

我们通过hibernate 4.1.12使用Java 1.6,Oracle 11g,驱动程序ojdbc6-11.2.0.3.0.jar

1 个答案:

答案 0 :(得分:0)

感谢提示didxga - 你让我们走向正确的方向。我们在classpath中添加了orai18n.jar,现在一切正常:)