java - 从 Java 将 REFCURSOR 类型变量发送到 Oracle 存储过程

标签 java oracle stored-procedures

我已经查遍了这个问题,但似乎找不到答案。如果此内容已发布,请指出该方向。

我有一个必须从 Java 调用的 Oracle 存储过程。此 SP 要求 REFCURSOR 类型作为 IN 参数(如我必须向其发送 REFCURSOR)。

我知道如何接收 REFCURSOR 作为来自 SP 的响应(使用 callablestatement 和 Stmt.registerOutParameter(3, OracleTypes.CURSOR);例如。这不是我想要的。

我无法修改 SP(很遗憾......)

我需要示例代码:

-在 Java 中创建 REFCURSOR

-将其作为参数发送给 SP。

非常感谢您提供的任何帮助。

最佳答案

来自Oracle Documentation :

Introduction to REF CURSORs

Using REF CURSORs is one of the most powerful, flexible, and scalable ways to return query results from an Oracle Database to a client application.

REF CURSOR is a PL/SQL data type whose value is the memory address of a query work area on the database. In essence, a REF CURSOR is a pointer or a handle to a result set on the database. REF CURSORs are represented through the OracleRefCursor ODP.NET class.

REF CURSORs have the following characteristics:

  • A REF CURSOR refers to a memory address on the database. Therefore, the client must be connected to the database during the lifetime of the REF CURSOR in order to access it.

  • A REF CURSOR involves an additional database round-trip. While the REF CURSOR is returned to the client, the actual data is not returned until the client opens the REF CURSOR and requests the data. Note that data is not be retrieved until the user attempts to read it.

  • A REF CURSOR is not updatable. The result set represented by the REF CURSOR is read-only. You cannot update the database by using a REF CURSOR.

  • A REF CURSOR is not backward scrollable. The data represented by the REF CURSOR is accessed in a forward-only, serial manner. You cannot position a record pointer inside the REF CURSOR to point to random records in the result set.

  • A REF CURSOR is a PL/SQL data type. You create and return a REF CURSOR inside a PL/SQL code block.

因此,您无法在 Java 中创建REF CURSOR;相反,您需要在数据库中创建它,将其传递给 Java 应用程序,然后将其传递回您正在调用的过程(或者全部在数据库中完成)。

关于java - 从 Java 将 REFCURSOR 类型变量发送到 Oracle 存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43710288/

相关文章:

sql - 按解释编译存储过程

mysql - 在MySQL存储过程中记录异常信息

java - TreeMap 以 TreeSet 为值,如何从该 TreeSet 中删除元素?

sql - 根据其他表分组分组

Java 在 updateLayoutState 时执行 Jar 文件 NullPointerException

sql - 从双表中选择确切的行数

c# - 是否有将 T-SQL 存储过程转换为 C# 的工具?

java - Oracle 包和 Java 包之间的映射

java - Spring Boot 中未找到映射

java - 数据截断:Incorrect value: error while inserting Date field to Mysql DB