简体   繁体   中英

Unable to retrieve Stored Procedure Return value through Java

I'm trying to retrieve results from stored procedures written in sql server 2008 through JPA. I've come across a very absurd situation in which I am able to retrieve results from stored procedures that have a select statement at the end. In this case, I'm able to retrieve the results by calling

storedProcedureQuery.getSingleResult();

However, if the stored procedure contains a return statement at the end returning a value, I'm unable to get any result and the same query returns a null pointer exception.

The stored procedure which returns a value looks like:-

CREATE PROCEDURE callSP
    @value int
    AS
BEGIN
    SET NOCOUNT ON;
    select 1;
END
GO

The stored procedures which causes an error looks like the one below:-

CREATE PROCEDURE callSP
    @value int
    AS
BEGIN
    SET NOCOUNT ON;
    RETURN 1;
END
GO

Is there some other way in which return values can be retrieved through JPA?

If you choose to return a value, you have to do:

CallableStatement storedProcedureQuery = con.prepareCall("{? = call my_procedure (?,?)}"); 

storedProcedureQuery.execute();
int groupId = cs.getInt(1);`

But, if you return a ResultSet, you have to do:

ResultSet rs = storedProcedureQuery.executeQuery();
if (rs.next())
    int groupId = rs.getInt(1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM