I should call stored procedure from the third party SQL Server data base (have rights for read only). Also, when I tried to execute this procedure for example in DataGrip:
EXEC Web.example_procedure 2, 3, 4
I received two results:
firs:
<anonymous>
-----------
3
second:
column_1 | column_2
------------------
k1 | v1
k2 | v2
k3 | v3
...
I need a second table.
Now I'm doing the next due to this 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();
}
}
Output is:
RESULT SET #1
In other words, I get only the first result. How to get the second table?
- can I modify SQL query? (Which will return only one table without first int result)
- JDBC?
- Hibernate?
I will be glad to any working variant.
Update
Thanks to @MarkRotteveel and his answer - I solved the problem
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();
}