CallableStatement.getResultSet() always return null when calling an Oracle function

房东的猫 提交于 2019-12-11 05:55:38

问题


I am executing the following code:

CallableStatement cs;
cs = conn.prepareCall("{ ? = call mypackage.myfunc()}");
cs.registerOutParameter(1, OracleTypes.CURSOR);
System.out.println(cs.execute());
System.out.println(cs.getResultSet());
ResultSet rs = (ResultSet) cs.getObject(1);
System.out.println(rs);

The function is declared as follows:

CREATE OR REPLACE PACKAGE BODY myuser.mypackage AS 
FUNCTION myfunc
return sys_refcursor 
is 
a_cursor sys_refcursor; 
begin 
open a_cursor for select * from mytable; 
return a_cursor; 
end myfunc; 
end mypackage; 
/ 

According to the documentation, if there is a result then cs.execute() will return true and cs.getResultSet() will have a value. However, this is the output I get:

false
null
oracle.jdbc.driver.OracleResultSetImpl@b92a848

I am using Oracle Express 11.2.0 and the latest driver.

Any hints/explanations/things to try will be very welcome.

Thanks!


回答1:


getResultSet() is typically used with PreparedStatements that return data from a SQL SELECT query, not with calls to stored procedures or functions. So I completely expect the false and null values you are seeing.

If you have a stored procedure that returns one or more ref cursors, then you fetch the values using getObject and cast them to ResultSets. In fact, your code above does exactly this, so I don't understand why you need 'things to try'.

A SQL SELECT statement has to send the selected data somewhere, but because you can't put a bind parameter or suchlike into the SQL to act as the destination for the data, you require a separate mechanism for getting access to this data via JDBC. This is what getResultSet() is for. Your function returns a ref cursor via a bind parameter, so there isn't any need for an implicit 'result', as you can access the data via the bind parameter.



来源:https://stackoverflow.com/questions/14946959/callablestatement-getresultset-always-return-null-when-calling-an-oracle-funct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!