我将首先向您提供我的表格内容
这是我的存储过程:
ALTER PROCEDURE dbo.getFoodsForOrder
(
@orderID INT,
@ID INT OUTPUT,
@food_restaurantID INT OUTPUT,
@count INT OUTPUT,
@description VARCHAR(200) OUTPUT
)
AS
SET NOCOUNT ON
BEGIN
SELECT [ID],
[food_restaurantID],
[count],
[description]
FROM Order_Food
WHERE orderID = @orderID
END
当我从这样的
中调用JDBC的存储过程时Connection con = Database.getConnection();
CallableStatement callableStatement = null;
try {
callableStatement = con
.prepareCall("{call getFoodsForOrder(?,?,?,?,?)}");
callableStatement.setInt(1, getID());
callableStatement.registerOutParameter(2, java.sql.Types.INTEGER);
callableStatement.registerOutParameter(3, java.sql.Types.INTEGER);
callableStatement.registerOutParameter(4, java.sql.Types.INTEGER);
callableStatement.registerOutParameter(5, java.sql.Types.VARCHAR);
System.out.println("ID = " + getID());
boolean haveResult = callableStatement.execute();
while (haveResult) {
System.out.println("here I am");
haveResult = callableStatement.getMoreResults();
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (callableStatement != null)
callableStatement.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
它只打印here I am
一次,只需一次,即使如果(如图所示)我有超过15行是真的。
你问我会问我是否确定getID()
方法,是的我确定,即使我用显式10替换它,结果也不会改变。
当我从Visual Studio调用该存储过程时,我得到了17个这样的结果:
答案 0 :(得分:3)
callableStatement.execute()
表示,如果结果集可用。
getMoreResults()
仅表示是否有另一个 ResultSet 可用,而不是“最后”结果集有更多行(这是 - 我认为 - 很清楚JavaDocs)。如果您的程序中有多个getMoreResults()
声明,则true
会返回select
。
如果execute()
表示ResultSet 可用,则需要使用callableStatement.getResultSet()
获取它,然后迭代它返回的行。
类似的东西:
boolean haveResult = callableStatement.execute();
if (haveResult) {
ResultSet rs = callableStatement.getResultSet();
while (rs.next()) {
System.out.println(rs.getInt(1));
}
} else {
System.out.println("Nothing returned");
}
但是,当您知道时,它会返回结果集,您也可以立即拨打getResultSet()
,而不必担心execute()
来电的返回值。
答案 1 :(得分:1)
您需要从存储过程中删除OUTPUT
变量,不再在代码中注册它们。然后,您需要使用executeQuery()
来获取结果集并迭代结果集以获取行。
假设表结构为:
CREATE TABLE [dbo].[Order_Food](
[ID] [int] IDENTITY(1,1) NOT NULL,
[OrderID] [int] NULL,
[food_restaurantID] [int] NULL,
[count] [int] NULL,
[description] [nvarchar](255) NULL,
CONSTRAINT [PK_Order_Food] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
表格内容:
INSERT INTO [dbo].[Order_Food]
([OrderID], [food_restaurantID], [count], [description])
VALUES (513, 2, 3, 'Description xyz'), (513, 2, 3, 'Description xyz'),
(132, 1, 2, 'Description abc');
存储过程为:
CREATE PROCEDURE dbo.getFoodsForOrder
(
@orderID INT
)
AS
SET NOCOUNT ON
BEGIN
SELECT [ID],
[food_restaurantID],
[count],
[description]
FROM Order_Food
WHERE orderID = @orderID
END
查询代码为:
public static void main(String[] args) throws SQLException {
try (
Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=Scratchspace;integratedSecurity=true");
CallableStatement callableStatement =
con.prepareCall("{call getFoodsForOrder(?)}");
){
callableStatement.setInt(1, 513);
System.out.println("ID = " + 513);
ResultSet rs = callableStatement.executeQuery();
while (rs.next()) {
System.out.printf("%d %d %d %s%n",
rs.getInt("ID"), rs.getInt("food_restaurantID"),
rs.getInt("count"), rs.getString("description"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
在我的电脑上生成输出:
ID = 513
1 2 3 Description xyz
2 2 3 Description xyz