source article
try (Connection c = DriverManager.getConnection(url, properties);
Statement s = c.createStatement()) {
try {
// First, we have to enable the DBMS_OUTPUT. Otherwise,
// all calls to DBMS_OUTPUT made on our connection won't
// have any effect.
s.executeUpdate("begin dbms_output.enable(); end;");
// Now, this is the actually interesting procedure call
s.executeUpdate("begin my_procedure(1, 2); end;");
// After we're done with our call(s), we can proceed to
// fetch the SERVEROUTPUT explicitly, using
// DBMS_OUTPUT.GET_LINES
try (CallableStatement call = c.prepareCall(
"declare "
+ " num integer := 1000;"
+ "begin "
+ " dbms_output.get_lines(?, num);"
+ "end;"
)) {
call.registerOutParameter(1, Types.ARRAY,
"DBMSOUTPUT_LINESARRAY");
call.execute();
Array array = null;
try {
array = call.getArray(1);
Stream.of((Object[]) array.getArray())
.forEach(System.out::println);
}
finally {
if (array != null)
array.free();
}
}
}
// Don't forget to disable DBMS_OUTPUT for the remaining use
// of the connection.
finally {
s.executeUpdate("begin dbms_output.disable(); end;");
}
}