2013-11-28 38 views
2

我寫了一個java代碼來調用存儲過程,它返回一些數據。以下是代碼 -異常:可調用語句沒有返回任何值

CallableStatement callableStatement = null; 
    List<OutputDTO> outputDTOList = new LinkedList<>(); 
    ResultSet rs = null; 
    String query = "{call Get_DailyCampaignReachReport (?,?,?,?,?,?,?,?,?)}"; 
    try { 
     callableStatement = connMan.getReportingDbConnection().prepareCall(query); 
     Integer[] data = inDTO.getCampaignId().toArray(new Integer[inDTO.getCampaignId().size()]); 

     callableStatement.setDate(1, new Date(inDTO.getStartDate().toDate().getTime())); 
     callableStatement.setDate(2, new Date(inDTO.getEndDate().toDate().getTime())); 
     callableStatement.setArray(3, connMan.getReportingDbConnection().createArrayOf("integer", data)); 
     callableStatement.setInt(4, inDTO.getNetworkId()); 

     callableStatement.registerOutParameter(5, java.sql.Types.DATE); 
     callableStatement.registerOutParameter(6, java.sql.Types.INTEGER); 
     callableStatement.registerOutParameter(7, java.sql.Types.INTEGER); 
     callableStatement.registerOutParameter(8, java.sql.Types.BIGINT); 
     callableStatement.registerOutParameter(9, java.sql.Types.BIGINT); 

     boolean results = callableStatement.execute(); 
     while (results) { 
      rs = callableStatement.getResultSet(); 
      while(rs.next()){ 
       OutputDTO outputDTO = new OutputDTO(); 
       outputDTO.setDate(new DateTime(rs.getDate(1).getTime())); 
       outputDTO.setCampaignId(rs.getInt(2)); 
       outputDTO.setNetworkId(rs.getInt(3)); 
       outputDTO.setUniques(rs.getInt(4)); 
       outputDTO.setTotal(rs.getInt(5)); 
       outputDTOList.add(outputDTO); 
      } 
      results = callableStatement.getMoreResults(); 
     } 
     collector.setOutputList(outputDTOList); 
    } catch (SQLException e) { 
     throw new InternalErrorException("runDayReport {" + query + "} - ", e); 
    } finally { 
     try { 

      if (null != rs) { 
       rs.close(); 
      } 

      if (null != callableStatement) { 
       callableStatement.close(); 
      } 

     } catch (SQLException ee) { 
      throw new InternalErrorException("Free Resources : runDayReport {" 
        + query + "} - " + ee); 
     } 
    } 

「Get_DailyCampaignReachReport」是存儲過程的名稱。現在當我執行這段代碼時,我得到一個異常「Callable語句沒有返回任何值。」在callablestatement上調用execute方法時會發生這種情況。但我無法理解爲什麼會發生這種情況。任何人都可以幫助我理解我犯的錯誤嗎?以下是存儲過程在數據庫中的外觀。

Schema |    Name    | Result data type |                     Argument data types                     | Type | Volatility | Owner | Language |                                 Source code                                 | Description 
--------+------------------------------+------------------+---------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------+--------+------------+----------+----------+------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------- ------------------+------------- 
    public | get_dailycampaignreachreport | SETOF record  | start_date date, end_date date, p_campaign_id integer[], p_network_id integer, OUT sqldate date, OUT campaign_id integer, OUT network_id integer, OUT uniques bigint, OUT total bigint | normal | volatile | postgres | plpgsql |                                                                    +| 
    |        |     |                                              |  |   |   |   | DECLARE                                                                  +| 
    |        |     |                                              |  |   |   |   | where_campaign_id text;                                                              +| 
    |        |     |                                              |  |   |   |   | in_values varchar default '';                                                             +| 
    |        |     |                                              |  |   |   |   | BEGIN                                                                   +| 
    |        |     |                                              |  |   |   |   | IF p_campaign_id NOTNULL THEN                                                             +| 
    |        |     |                                              |  |   |   |   | FOR i IN 1..array_upper(p_campaign_id, 1) LOOP                                                        +| 
    |        |     |                                              |  |   |   |   | in_values := in_values || p_campaign_id[i] || ',';                                                       +| 
    |        |     |                                              |  |   |   |   | END LOOP;                                                                  +| 
    |        |     |                                              |  |   |   |   | in_values := substring(in_values FROM 1 FOR character_length(in_values) - 1);                                                +| 
    |        |     |                                              |  |   |   |   | where_campaign_id := ' campaign_id IN (' || in_values || ')' ;                                                    +| 
    |        |     |                                              |  |   |   |   | END IF;                                                                  +| 
    |        |     |                                              |  |   |   |   | RETURN QUERY EXECUTE 'SELECT sqldate,campaign_id,network_id,users,total FROM campaign_uniques_daily WHERE sqldate BETWEEN ' || quote_literal(start_date) || ' AND ' || quote_literal(end_date) || ' AND network_id = ' || p_network_id || ' AND ' || where_campaign_id || ' ';+| 
    |        |     |                                              |  |   |   |   | END;   
+0

查看[使用帶有OUT參數的CallableStatement的示例](http://www.mkyong.com/jdbc/jdbc-callablestatement-stored-procedure-out-parameter-example/)。 – SudoRahul

+0

嘿,我檢查了這篇文章,但我如何管理循環。我不會從數據庫中獲取單行,而是多行。 – user1305398

+0

你錯誤地解釋了'execute'和'getMoreResults'的返回值。也取決於存儲過程的類型,值可能實際上是通過'CallableStatement'本身返回的,而不是'ResultSet'。 –

回答

1

您使用的不是結果參數,即

{?= call <procedure-name>[(<arg1>,<arg2>, ...)]} 

,但如果你使用這種形式,那麼你的存儲過程可能return a cursor

相關問題