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

时间:2014-10-18 08:08:10

标签: java sql-server-2008 jdbc

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

StoredProcedure的

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

1 个答案:

答案 0 :(得分:1)

  

该声明未返回结果集。

这是事实。程序

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