jason.kaisersmith
jason.kaisersmith

Reputation: 9650

Reading DBMS_Ouptut after Executing PLSQL from jdbc

I need to execute an Oralce PL/SQL statement using jdbc, but I'm struggling on how to extract the data.

Here is my statement

DECLARE 
   l_xmltype XMLTYPE; 
   l_ctx dbms_xmlgen.ctxhandle; 
BEGIN 
   l_ctx := dbms_xmlgen.newcontext('select * from myTable where rownun < 10;); 
   dbms_xmlgen.setrowsettag(l_ctx, 'My_DATA'); 
   dbms_xmlgen.setrowtag(l_ctx, 'My_Element'); 
   l_xmltype := dbms_xmlgen.getXmlType(l_ctx) ; 
   dbms_xmlgen.closeContext(l_ctx);  
  dbms_output.put_line(l_xmltype.getClobVal); 
  dbms_output.get_lines(?, l_xmltype);
End;

And my code

CallableStatement cs = connection.prepareCall(plsql);
cs.registerOutParameter(1, Types.ARRAY,"DBMSOUTPUT_LINESARRAY");
cs.execute();

Array array = null;
array = cs.getArray(1);
Stream.of((Object[]) array.getArray())
    .forEach(System.out::println);

And I'm getting the error

java.sql.SQLException: ORA-06550: line 1, column 380: PLS-00306: wrong number or types of arguments in call to 'GET_LINES'

I'm not an expert in PL/SQL nor jdbc so I'm struggling to find a solution.

Upvotes: 2

Views: 1179

Answers (1)

Lukas Eder
Lukas Eder

Reputation: 221370

The second argument of GET_LINES is a number, not an XMLTYPE. Change your call to something like:

dbms_output.get_lines(?, 50);

I've also shown a more complete example on how to do this in a similar question. Note that you also have to add the following call first, to enable dbms_output collection:

dbms_output.enable();

Note that in case you're using jOOQ, you can automatically fetch server output on any statement.

Upvotes: 1

Related Questions