Get multiple result sets with JDBC doesn't work

I have to call a stored procedure from a third party SQL Server database (you have read-only permissions ). Also, when I tried to follow this procedure, for example in DataGrip:

EXEC Web.example_procedure 2, 3, 4

I got two results:

fir:

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

      

second:

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

      

I need a second table.

Now I am doing the following because of 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:

RESULT SET #1

In other words, I only get the first result. How do I get the second table?

  • Can the SQL query be modified? (Which will only return one table without the first int result)
  • JDBC?
  • Hibernate?

I will be happy with any working option.

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();
}

      

+3


source to share


2 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

+1


source


You can install an updatable result set to run multiple commands.



Statement stmt = conn1.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);

        String insert1="insert into data values('******','*********')";
        String insert2="insert into data values('*******','******')";
        conn1.setAutoCommit(false);

        ResultSet rs = stmt.executeQuery("select * from data");
        rs.last();

      

0


source







All Articles