gpt4 book ai didi

java - 使用 JDBC 获取多个结果集不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:50:10 25 4
gpt4 key购买 nike

<分区>

我应该从第三方 SQL Server 数据库调用存储过程(具有只读权限)。此外,当我尝试在 DataGrip 中执行此过程时:

EXEC Web.example_procedure 2、3、4

我收到了两个结果:

先:

<anonymous>
-----------
3

第二个:

column_1 | column_2
------------------
k1 | v1
k2 | v2
k3 | v3
...

我需要第二张 table 。

由于这个 article,现在我正在做下一步

private static void executeStatement(Connection con) {
try {
String SQL = "EXEC Web.example_procedure 2, 3, 4";
Statement stmt = con.createStatement();
boolean results = stmt.execute(SQL);
int rsCount = 0;

//Loop through the available result sets.
do {
if (results) {
ResultSet rs = stmt.getResultSet();
rsCount++;

//Show data from the result set.
System.out.println("RESULT SET #" + rsCount);
while (rs.next()) {
// something will be here
}
rs.close();
}
results = stmt.getMoreResults();
} while (results);
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}

输出是:

结果集#1

换句话说,我只得到第一个结果。如何获取第二个表?

  • 我可以修改 SQL 查询吗? (这将只返回一张没有第一个整数结果)
  • JDBC?
  • hibernate ?

我会很高兴任何工作变体。

更新

感谢@MarkRotteveel 和他的 answer - 我解决了问题

String sql = "EXEC Web.example_procedure 2, 3, 4";
PreparedStatement stmt = con.prepareStatement(sql);

boolean result = stmt.execute();

while (true) {
if (result) {
ResultSet rs = stmt.getResultSet();

// in my case first table has only one column,
// and I need the second table, which has 9 columns
if (rs.getMetaData().getColumnCount() > 1) {

// go through the rows
while (rs.next()) {

// for example what we can do
rs.getMetaData().getColumnCount(); // return column count in the current result set
rs.getObject(int columnIndex); // get value for column index. Must be not greater than .getColumnCount()
}
}

} else {
int updateCount = stmt.getUpdateCount();
if (updateCount == -1) {
// no more results
break;
}
}
result = stmt.getMoreResults();
}

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