7

By "Messages" I mean the Messages tab in the following figure, which includes "Warning", "n rows affected" and in the figure below, the execution time. In JDBC, ResultSet class is used to retrieve the "Results". Is there any way to get all the information in the "Message" tab through JDBC?

enter image description here

2 Answers 2

10

You can get most of those messages, but unfortunately not all. See my question on Stackoverflow regarding that.

In general those messages (e.g. messages from a PRINT statement) are returned as warnings on the Statement object by the JDBC driver. To retrieve them use Statement.getWarnings() in a loop:

Statement stmt = ...;
stmt.execute("some_procedure");

SQLWarning warning = stmt.getWarnings();

while (warning != null)
{
   System.out.println(warning.getMessage());
   warning = warning.getNextWarning();
}

I have seen some messages also being returned on the Connection instance rather than the Statement. If you don't see anything, try the above code with your Connection instance.

1
  • Thanks for your reply. I've tried but it seems only the print stmt returns a msg. I'd like to show the execution time by executing "set statistics time on" but I didn't see anything, either by stmt or Connection instance. I was wondering if you've ever seen message returned by other statements other than PRINT statement?
    – zli89
    Jun 12, 2013 at 19:16
2

I was looking for a way to do this (except the execution times) and I have managed to achieve this using the version 7.4.1 of their JDBC driver.

do {
    resultSet = statement.getResultSet();
    updateCount = statement.getUpdateCount();
    printWarnings(statement.getWarnings());
    statement.clearWarnings();
    if (resultSet != null) {
        printResults(resultSet);
    }
    if (updateCount != -1) {
        printError(updateCount + " row(s) affected/returned");
    }
}
while (statement.getMoreResults() || updateCount != -1);
printWarnings(statement.getWarnings());

Where printWarnings handles the warning chain. It works for the cases I have tested. You can get the execution time by sending SET STATISTICS TIME ON before the given query, then it will come in the form of an SQLWarning. Generally you want to disable it after using SET STATISTICS TIME OFF.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.