What is the equivalent of Oracle’s REF CURSOR in MySQL when using JDBC?

别等时光非礼了梦想. 提交于 2019-11-27 23:22:11

Mysql has an implicit cursor that you can magically return from a stored procedure if you issue a select.

Here's an example:

CREATE PROCEDURE `TEST`()
MODIFIES SQL DATA
BEGIN
  SELECT * FROM test_table;
END;

and in your java code:

String query = "{CALL TEST()}";
CallableStatement cs = con.prepareCall(query,
    ResultSet.TYPE_SCROLL_INSENSITIVE,
    ResultSet.CONCUR_READ_ONLY);
ResultSet rs = cs.executeQuery();

Googling on cursors in MySQL, it doesn't seem like you can actually return a Cursor from a Proc or Function. Additionally, I found the following in the MySQL JDBC manual:

"MySQL does not support SQL cursors, and the JDBC driver doesn't emulate them, so "setCursorName()" has no effect."

In general, I believe Oracle's implementation here breaks JDBC, and is not used elsewhere (MySQL, MSSQL, etc). You should be returning your results as a select statement and iterating over the JDBC ResultSet, as is standard (and intended) practice when using JDBC.

fill a temporary table in a procedure and just read the temporary table... :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!