我有一个应该返回代码的存储过程。 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
调用此SP的Java类:
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.
任何人都可以帮助我吗?
答案 0 :(得分:1)
尝试将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);
这里有一些相关主题: