我有这个存储过程:
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
调用它。
如何获取变量输出ID
和name
的所有值,因为存储过程会重新获得它们的许多值。
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));
我刚刚收到ID
和name
的最后一个值。
任何帮助,请
答案 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