从PLSQL过程中获取值列表

时间:2016-10-13 12:26:18

标签: java oracle plsql

我是Java和PLSQL的新手。我在PLSQL中构建了这个过程:

    PROCEDURE getLogs (
    p_idcontract            IN          NUMBER,
    p_iduser                IN          NUMBER,
    o_logs                  OUT         VARCHAR2
  )
  IS   
  BEGIN
      SELECT logData INTO o_logs
      FROM SERVICELOG 
      WHERE IDCONTRACT= p_idcontract 
      AND IDUSER= p_iduser;

  END getLogs;

如果脚本检测到多行,请返回发送此消息的Java类:

  

ORA-01422:精确提取返回超过请求的行数

请问,我如何返回一个hashmap或类似的,准备输出到Java?感谢。

1 个答案:

答案 0 :(得分:1)

我会使用ref cursor作为输出参数,然后在java程序中构建map。 https://oracle-base.com/articles/misc/using-ref-cursors-to-return-recordsets 您还可以在oracle中定义类似Java Array的用户类型。

    PROCEDURE getLogs (
    p_idcontract            IN          NUMBER,
    p_iduser                IN          NUMBER,
    p_recordset OUT SYS_REFCURSOR
  )
  IS   
  BEGIN
 OPEN p_recordset FOR
      SELECT logData 
      FROM SERVICELOG 
      WHERE IDCONTRACT= p_idcontract 
      AND IDUSER= p_iduser;

  END getLogs;

Java代码:

CallableStatement stmt = conn.prepareCall("BEGIN getLogs(?, ?, ?); END;");
      stmt.setInt(1, 0);
      stmt.setInt(2, 0);
      stmt.registerOutParameter(3, OracleTypes.CURSOR);
      stmt.execute();
      ResultSet rs = ((OracleCallableStatement)stmt).getCursor(3);
      while (rs.next()) {
        ;
       //here build your Map, list or whatever you want
      }