If I run a PostgreSQL command that generates output as a stream of text, such as vacuum full verbose analyze, how do I capture its output using a JDBC connection?
1 Answer
This is available in the warnings of the Statement object that was used to run the command:
Statement stmt = connection.createStatement();
stmt.execute("vacuum verbose foobar");
SQL Warning warn = stmt.getWarnings();
if (warn != null)
{
System.out.println(warn.getMessage);
}
Theoretically there can be chained warnings, so you might want to use a loop, that checks SQLWarning.getNextWarning(). Check the JDK JavaDocs for more details.