SP 不返回结果集

SP not returning resultset

我有一个存储过程,它应该 return 一个代码。 PFB样本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 调用此 SP 的位置:

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;
    }

我在 运行 时遇到异常。异常发生在 ct.executeQuery(); 并表示

com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set.

谁能帮帮我?

尝试将 return 参数作为调用的标量结果,如下所示:

    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);

这里有一些相关主题:

Getting the Return Value from JDBC MSSQL

How to call scalar function from JDBC