
使用JDBC 檢索DBMS_OUTPUT.get_lines
在Java 應用程式中使用Oracle 的dbms_output.get_lines 時,JJM的方法,而無需建立附加資料庫物件。此方法涉及一系列步驟:
以下是說明此過程的範例程式碼片段:
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 相比,基準測試顯示速度減慢速度減慢. ,即使在PL/SQL 中也是如此。因此,為了提高效率,建議使用 DBMS_OUTPUT.GET_LINES 的批次方法。
以上是如何使用 JDBC 檢索 Oracle 的 DBMS_OUTPUT?的詳細內容。更多資訊請關注PHP中文網其他相關文章!