使用 JDBC 检索 DBMS_OUTPUT.get_lines
在 Java 应用程序中使用 Oracle 的 dbms_output.get_lines 时,JDBC 提供了一种获取输出的方法,而无需创建附加数据库对象。此方法涉及一系列步骤:
下面是说明此过程的示例代码片段:
try (CallableStatement call = c.prepareCall( "declare " + " num integer := 1000;" // Adapt this as needed + "begin " + " dbms_output.enable();" + " dbms_output.put_line('abc');" + " dbms_output.put_line('hello');" + " dbms_output.put_line('so cool');" // This captures the output up until now through a PL/SQL TABLE type. // Oracle 12c+ uses a SQL cursor, while 11g requires an auxiliary SQL TABLE + " dbms_output.get_lines(?, num);" // Disable buffering + " 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(); } }
使用 jOOQ
如果使用 jOOQ 库,您可以通过在“设置”中启用它来自动检索服务器输出以进行查询object:
DSLContext ctx = DSL.using(c, new Settings().withFetchServerOutputSize(10));
查询执行后,服务器输出将在 ExecuteContext::serverOutput 中可用。
注意 DBMS_OUTPUT.GET_LINE
虽然 DBMS_OUTPUT.GET_LINE 单独检索行,但与使用 DBMS_OUTPUT.GET_LINE 相比,基准测试显示速度显着减慢DBMS_OUTPUT.GET_LINES,即使在 PL/SQL 中也是如此。因此,为了提高效率,建议使用 DBMS_OUTPUT.GET_LINES 的批量方法。
以上是如何使用 JDBC 检索 Oracle 的 DBMS_OUTPUT?的详细内容。更多信息请关注PHP中文网其他相关文章!