Skip to content

Instantly share code, notes, and snippets.

@funkman
Created September 6, 2018 12:02
Show Gist options
  • Save funkman/9c48dbb2452a8dd4fe9e147b9849aa67 to your computer and use it in GitHub Desktop.
Save funkman/9c48dbb2452a8dd4fe9e147b9849aa67 to your computer and use it in GitHub Desktop.
Capture DBMS_OUTPUT.PUT_LINE in java (actually groovy)
Connection con = getYourDbConnectionObject();
/* Enable the capturing of the buffer */
Statement s = con.createStatement()
s.executeUpdate("begin dbms_output.enable(); end;");
s.close();
/* Run your proc */
CallableStatement cs = con.prepareCall("{call MY_STORED_PROC_HERE}")
cs.execute()
cs.close()
// con.commit(); /* If needed */
/* Output the buffer */
int arrayLength = 10000; // Or whatever length makes sense
cs = con.prepareCall("{call dbms_output.get_lines(?, ?)}")
cs.registerOutParameter(1, Types.ARRAY, "DBMSOUTPUT_LINESARRAY");
cs.registerOutParameter(2, Types.INTEGER);
cs.setInt(2, arrayLength);
cs.execute()
Array array = cs.getArray(1);
println array.getArray().join("\n")
/* For the OCD ... The above will print an extra null at the end */
array.free();
cs.close();
/* Disable output (not needed if you are closing the connection now) */
Statement s = con.createStatement()
s.executeUpdate("begin DBMS_OUTPUT.DISABLE(); end;");
s.close();
con.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment