Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get multiple result sets with JDBC doesn't work [duplicate]

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();
}
like image 471
Optio Avatar asked Jul 10 '26 11:07

Optio


1 Answers

Use JDBC CallableStatement:

cstmt.registerOutParameter()
cstmt.getObject()

  String sql = "{call getEmpName (?, ?)}";
  cstmt = conn.prepareCall(sql);

  //Bind IN parameter first, then bind OUT parameter
  int empID = 102;
  cstmt.setInt(1, empID); // This would set ID as 102
  // Because second parameter is OUT so register it
  cstmt.registerOutParameter(2, OracleTypes.CURSOR);

  //Use execute method to run stored procedure.
  System.out.println("Executing stored procedure..." );
  cstmt.execute();

  //Retrieve data
  rs = (ResultSet) cstmt.getObject(1);

https://docs.oracle.com/cd/E17952_01/connector-j-en/connector-j-usagenotes-statements-callable.html

like image 188
lihongxu Avatar answered Jul 14 '26 06:07

lihongxu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!