从存储过程中获取多重值

时间:2013-04-29 07:28:52

标签: java sql-server stored-procedures jdbc sql-server-2008-r2

我有这个存储过程:

ALTER PROCEDURE dbo.getRegionsInArea
    (
    @areaID int,
    @ID int OUTPUT,
    @name varchar(50) OUTPUT
    )
AS
BEGIN
    SET NOCOUNT ON
    SELECT @ID = [ID], @name = [name] FROM Region
    WHERE areaID = @areaID
    END

我想从JDBC调用它。

questiong

如何获取变量输出IDname的所有值,因为存储过程会重新获得它们的许多值。

我尝试了什么

Connection con = Database.getConnection();
            String storedProcedure = "{call getRegionsInArea (?,?,?)}";
            CallableStatement callableStatement = null;
            try {
                callableStatement = con.prepareCall(storedProcedure);
                callableStatement.setInt(1, getID());
                callableStatement.registerOutParameter(2,
                        java.sql.Types.INTEGER);
                callableStatement.registerOutParameter(3,
                        java.sql.Types.VARCHAR);
                callableStatement.execute();
                    System.out.println("ID = " + callableStatement.getInt(2));
                    System.out.println("name = "
                            + callableStatement.getString(3));

问题

我刚刚收到IDname的最后一个值。

任何帮助,请

1 个答案:

答案 0 :(得分:2)

尝试使用此解决方案:

callableStatement = con.prepareCall(storedProcedure); 
callableStatement.setInt(1, getID()); 
callableStatement.execute(); 
ResultSet resultSet = callableStatement.getResultSet(); 
while (resultSet.next()) { 
   int returnedID = resultSet.getInt(1); 
   String returnedName = resultSet.getString(2);   
}

并改变你的SP:

ALTER PROCEDURE dbo.getRegionsInArea 
( 
@areaID int
) 
AS 
BEGIN 
SET NOCOUNT ON 
SELECT [ID],[name] FROM Region 
WHERE areaID = @areaID 
END