简体   繁体   中英

Unable to fetch Resultset from StoredProc in Java

We are trying to execute stored proc in Java which looks like as below,

CREATE PROCEDURE [dbo].[MyProc] 
(
    @input1 varchar(20),
    @input2 varchar(20),
    @responsecode       varchar(10) output
)
AS

//Implementation

SELECT @a,@b,@c,@d;

SELECT @responsecode;

Java Code looks like as below,

callableStatement = conn.prepareCall("{call MyProc("2","2"))}");
callableStatement.registerOutParameter(3, Types.VARCHAR);           
callableStatement.registerOutParameter(4,Types.JAVA_OBJECT);

As you can see , we are trying to get two output parameters from stored proc,

We are able to get response code at index 3 , however @a,@b,@c,@d should come as resulset at index 4 ( may be I am wrong)

Can you please give me some poninters on getting resultset for such kind of stored proc.

when I execute this stored proc in Microsoft SQL Server Managment Studio, I can see two types of responses, first is resultset and other one is ResponseCode as below,

在此输入图像描述

You're mixing up resultsets with output parameters. The stored procedure in your post only has one output parameter, @responsecode .

Output parameters do not need to be selected out after you set them in a proc:

create procedure testsp (@param int output)
as
    set @param = 1

GO

declare @param int

exec testsp @param output


select @param

So remove the SELECT @responsecode; , and also the 4th registered parameter in your callable statement.

To get to your data, iterate over the resultset returned by executeQuery() .

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