Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
353 views
in Technique[技术] by (71.8m points)

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

In Oracle I can declare a reference cursor...

TYPE t_spool IS REF CURSOR RETURN spool%ROWTYPE;

...and use it to pass a cursor as the return value...

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;

...and then capture it as a result set using 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);

What is the equivalent in MySQL?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...