User Tools

Site Tools


wiki:tech:java-hints:java-and-oracle

read dbms_output in jdbc

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;");
    }
}
wiki/tech/java-hints/java-and-oracle.txt · Last modified: 2019/04/09 19:34 by kpc

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki