gpt4 book ai didi

java - sql server 2008,PreparedStatement jdbc 获取多个查询

转载 作者:行者123 更新时间:2023-12-01 12:24:56 25 4
gpt4 key购买 nike

给出错误:com.microsoft.sqlserver.jdbc.SQLServerException:该语句未返回结果集。

存储过程

CREATE  PROCEDURE getDetails(@n varchar(50) OUT,@c varchar(50) OUT,@i INT OUT)
as
BEGIN
SELECT @n=product.name,@c=product.code,@i=product.id FROM product;
END;

java代码

 try
{
Connection con =LoginConnection.getConnection();
CallableStatement stmt=con.prepareCall("{call getDetails(?,?,?)}");

stmt.registerOutParameter(1, Types.VARCHAR);
stmt.registerOutParameter(2, Types.VARCHAR);
stmt.registerOutParameter(3, Types.INTEGER);
ResultSet resultSet = stmt.executeQuery();
while(resultSet.next())
{
System.out.println("value 1:"+resultSet.getString(1));
System.out.println("value 2:"+resultSet.getString(2));
System.out.println("Value 3:"+resultSet.getInt(3));
}

con.close();
}
catch(Exception e)
{
System.out.println("ex:"+e);
}

最佳答案

The statement did not return a result set.

确实如此。流程

CREATE PROCEDURE getDetails
(
@n varchar(50) OUT,
@c varchar(50) OUT,
@i INT OUT
) AS
BEGIN
SELECT @n=product.name,@c=product.code,@i=product.id FROM product;
END;

不会返回结果集。它只会从表的一行(SELECT 语句返回的最后一行)返回三个标量 OUT 参数值。如果您希望存储过程返回结果集,那么

ALTER PROCEDURE getDetails
AS
BEGIN
SELECT product.name, product.code, product.id FROM product;
END;

并使用此 Java 代码

CallableStatement stmt = con.prepareCall("{call getDetails}");
ResultSet resultSet = stmt.executeQuery();
while (resultSet.next()) {
System.out.println("ProductName: " + resultSet.getString(1));
System.out.println("ProductCode: " + resultSet.getString(2));
System.out.println(" ProductID: " + resultSet.getInt(3));
}

关于java - sql server 2008,PreparedStatement jdbc 获取多个查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26437637/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com