I have a Stored Procedure which should return a code. PFB the sample SP.
USE [TEST]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[test_sp]
-- Add the parameters for the stored procedure here
@id int,
@name varchar(25),
@return_code int OUTPUT
AS
BEGIN
select @return_code=1
return @return_code
END
Java class where this SP is being called:
public int callSP() {
Connection con = getConnection();
ResultSet rs = null;
CallableStatement ct = null;
int returnCode = -1;
try {
ct = con.prepareCall("{call test_sp (?,?,?)}");
ct.setInt(1, 1);
ct.setString(2, "divya");
ct.registerOutParameter(3, java.sql.Types.INTEGER);
ct.executeQuery();
returnCode = ct.getInt(3);
if (returnCode == 0) {
System.out.println("Success");
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
database.close(rs, ct, con);
}
return returnCode;
}
I am getting an exception while running this. The exception occurs on ct.executeQuery();
and says that,
com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.
Can anyone help me out?
Try to place the return parameter as the scalar result of the call, like this :
ct = con.prepareCall("{? = call test_sp (?,?)}");
ct.registerOutParameter(1, java.sql.Types.INTEGER);
ct.setInt(2, 1);
ct.setString(3, "divya");
ct.execute();
returnCode = ct.getInt(1);
Some related topics here :