异常:可调用语句未返回任何值
Posted
技术标签:
【中文标题】异常:可调用语句未返回任何值【英文标题】:Exception : Callable statement did not return any value 【发布时间】:2013-11-28 05:16:46 【问题描述】:我编写了一个 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”是存储过程的名称。现在,当我执行此代码时,我得到一个异常“可调用语句没有返回任何值”。当在 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;
【问题讨论】:
看看an example of using CallableStatement with OUT parameters。 嘿,我查看了这篇文章,但我该如何管理循环。我不会从数据库中获取单行,而是多行。 您误解了execute
和getMoreResults
的返回值。此外,根据存储过程的类型,值实际上可能通过 CallableStatement
本身返回,而不是作为 ResultSet
。
只需添加一件事我正在使用 postgres DB。
【参考方案1】:
您没有使用结果参数,即
?= call <procedure-name>[(<arg1>,<arg2>, ...)]
但如果您要使用该表单,那么您的存储过程可能会return a cursor。
【讨论】:
以上是关于异常:可调用语句未返回任何值的主要内容,如果未能解决你的问题,请参考以下文章
使用 JDBC 可调用 stmt 存储 proc 时返回的结果集的顺序是啥?