DBMS_OUTPUT aus JDBC zu lesen ist durchaus machbar. Hier ein Ausschnitt, der die Serverausgabe in einem Durchgang erfasst:
try (CallableStatement call = c.prepareCall( "declare " + " num integer := 1000;" // Adapt this as needed + "begin " // You have to enable buffering any server output that you may want to fetch + " dbms_output.enable();" // This might as well be a call to third-party stored procedures, etc., whose // output you want to capture + " dbms_output.put_line('abc');" + " dbms_output.put_line('hello');" + " dbms_output.put_line('so cool');" // This is again your call here to capture the output up until now. // The below fetching the PL/SQL TABLE type into a SQL cursor works with Oracle 12c. // In an 11g version, you'd need an auxiliary SQL TABLE type + " dbms_output.get_lines(?, num);" // Don't forget this or the buffer will overflow eventually + " dbms_output.disable();" + "end;" )) { call.registerOutParameter(1, Types.ARRAY, "DBMSOUTPUT_LINESARRAY"); call.execute(); Array array = null; try { array = call.getArray(1); System.out.println(Arrays.asList((Object[]) array.getArray())); } finally { if (array != null) array.free(); } } Das gibt aus:
[abc, hello, so cool, null] Da die ENABLE/DISABLE-Einstellung verbindungsweit gilt, können Sie dies auch auf mehrere Anweisungen verteilen:
try (Connection c = DriverManager.getConnection(url, properties); Statement s = c.createStatement()) { try { s.executeUpdate("begin dbms_output.enable(); end;"); s.executeUpdate("begin dbms_output.put_line('abc'); end;"); s.executeUpdate("begin dbms_output.put_line('hello'); end;"); s.executeUpdate("begin dbms_output.put_line('so cool'); end;"); 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); System.out.println(Arrays.asList((Object[]) array.getArray())); } finally { if (array != null) array.free(); } } } finally { s.executeUpdate("begin dbms_output.disable(); end;"); } } Beachten Sie, dass dies höchstens 1.000 Zeilen abruft; für mehr müssten Sie in PL/SQL schleifen oder pollen.
Ein Hinweis zu GET_LINE vs GET_LINES
Es gab einmal eine Antwort, die einzelne DBMS_OUTPUT.GET_LINE-Aufrufe vorschlug, die jeweils eine Zeile zurückgeben. Ich habe beide Ansätze gebenchmarkt, und der Unterschied ist gravierend – bis zu 30-fach langsamer aus JDBC, obwohl der Abstand beim Aufruf aus PL/SQL vernachlässigbar ist. Der Bulk-Ansatz mit DBMS_OUTPUT.GET_LINES lohnt sich also klar. Das Benchmark finden Sie hier:
https://blog.jooq.org/2017/12/18/the-cost-of-jdbc-server-roundtrips/