gpt4 book ai didi

java - 从存储过程中获取乘法值

转载 作者:行者123 更新时间:2023-12-02 07:04:21 26 4
gpt4 key购买 nike

我有这个存储过程:

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 调用它。

提问

如何获取变量输出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 的最后一个值。

请帮忙

最佳答案

尝试使用此解决方案:

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

关于java - 从存储过程中获取乘法值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16273317/

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