在Oracle中我可以声明一个引用游标......
TYPE t_spool IS REF CURSOR RETURN spool%ROWTYPE;
...并使用它将光标作为返回值传递...
FUNCTION end_spool RETURN t_spool AS v_spool t_spool; BEGIN COMMIT; OPEN v_spool FOR SELECT * FROM spool WHERE key = g_spool_key ORDER BY seq; RETURN v_spool; END end_spool;
...然后使用JDBC将其作为结果集捕获...
private Connection conn; private CallableStatement stmt; private OracleResultSet rset; [...clip...] stmt = conn.prepareCall("{ ? = call " + call + "}"); stmt.registerOutParameter(1, OracleTypes.CURSOR); stmt.execute(); rset = (OracleResultSet)stmt.getObject(1);
MySQL中的等价物是什么?
Mysql有一个隐式游标,如果你发出一个select,你可以从存储过程中神奇地返回.
这是一个例子:
CREATE PROCEDURE `TEST`() MODIFIES SQL DATA BEGIN SELECT * FROM test_table; END;
在你的java代码中:
String query = "{CALL TEST()}"; CallableStatement cs = con.prepareCall(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = cs.executeQuery();