Fetching DBMS_OUTPUT Using JDBC
When handling database procedures or functions using Java and JDBC, there may be a need to capture output without creating additional objects or altering the database schema. One approach involves utilizing Oracle's DBMS_OUTPUT package.
To retrieve output from DBMS_OUTPUT.get_lines, a JDBC call can be made to a stored procedure like the one below, which includes the necessary steps for enabling buffering, outputting lines, retrieving the lines, and disabling buffering:
declare num integer := 1000; begin dbms_output.enable(); dbms_output.put_line('abc'); dbms_output.put_line('hello'); dbms_output.put_line('so cool'); dbms_output.get_lines(?, num); dbms_output.disable(); end;
Within the JDBC code, this procedure can be called using a CallableStatement and the getArray method to fetch the output as an array:
CallableStatement call = c.prepareCall(procedureQuery); call.registerOutParameter(1, Types.ARRAY, "DBMSOUTPUT_LINESARRAY"); call.execute(); Array array = call.getArray(1); System.out.println(Arrays.asList((Object[]) array.getArray()));
Another advantage of using DBMS_OUTPUT.get_lines is its bulk transfer capability, significantly reducing the overhead of individual DBMS_OUTPUT.get_line calls. Benchmarking has shown that the bulk approach can be up to 30 times faster than the individual line approach when executed from JDBC.
The above is the detailed content of How Can I Efficiently Fetch Oracle's DBMS_OUTPUT Using JDBC?. For more information, please follow other related articles on the PHP Chinese website!