CURSOR作为JDBC数据类型

CURSOR作为JDBC数据类型

本文介绍了CURSOR和REF CURSOR作为JDBC数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多RDBMS支持某种"CURSOR"类型.从存储过程返回时,这些类型最有用. Oracle中的一个示例:

Many RDBMS support "CURSOR" types of some sort. Those types are mostly useful when returned from stored procedures. An example in Oracle:

TYPE t_cursor_type IS REF CURSOR;
CREATE PROCEDURE p (c OUT t_cursor_type);

使用JDBC调用此过程时,应使用OracleTypes.CURSOR = -10"JDBC"类型.此类型不是任何标准的一部分,也不会成为Java 7中JDBC 4.1的一部分.

When calling this procedure using JDBC, the OracleTypes.CURSOR = -10 "JDBC" type should be used. This type is not part of any standard and it is not going to be part of JDBC 4.1 in Java 7.

有人知道JSR家伙将来是否会考虑将这种类型添加到标准中?还是其他RDBMS具有类似的特定于供应商的类型"?

Does anyone know whether the JSR guys will consider adding this type to the standard some time in the future? Or if other RDBMS have a similar "vendor-specific type"?

推荐答案

在Java 8/JDBC 4.2中添加了对REF CURSORS的支持.使用 Types.REF_CURSOR 类型游标返回类型.可以通过ResultSet接口对其进行迭代.示例:

Support for REF CURSORS was added in Java 8/JDBC 4.2. Use the type Types.REF_CURSOR for cursor return types. They can be iterated through the ResultSet interface. Example:

CallableStatement cstmt = conn.prepareCall("{callmySproc(?)}");
cstmt.registerOutParameter(1, Types.REF_CURSOR);
cstmt.executeQuery();
ResultSet cursor = cstmt.getObject(1, ResultSet.class);
while(cursor.next()) {
    System.out.println("Name = " + cursor.getString(1));
}

这篇关于CURSOR和REF CURSOR作为JDBC数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:35