简体   繁体   English

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

[英]sql server 2008 ,PreparedStatement jdbc to fetch multiple queries

gives error : com.microsoft.sqlserver.jdbc.SQLServerException: The statement did not return a result set. 给出错误:com.microsoft.sqlserver.jdbc.SQLServerException:语句未返回结果集。

StoredProcedure 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 code 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. 该语句未返回结果集。

That is true. 那是真实的。 The procedure 步骤

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;

will not return a resultset. 将不会返回结果集。 It will only return the three scalar OUT parameter values from a single row of the table (the last row returned by the SELECT statement). 它将仅从表的单行(SELECT语句返回的最后一行)返回三个标量OUT参数值。 If you want the stored procedure to return a resultset then 如果要存储过程返回结果集,则

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

and use this Java code 并使用此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));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM