<分区>
我应该从第三方 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();
}