A cursor is a data processing method that allows you to browse one or more rows forward or backward in a result set at a time, for you to view or process data in the result set.
The following example shows how to use useCursorFetch to return a cursor result set:
Establish a link and set parameters
useCursorFetchanduseServerPrepStmts.String url = "jdbc:mysql://host:port/test?useServerPrepStmts=false&useCursorFetch=true"conn = DriverManager.getConnection(url,"admin@mysql", "admin");Note
If
useServerPrepStmts=false, whenuseCursorFetchis set totrue,useServerPrepStmtsis forcibly changed totrue.
Generate a prepared statement.
PreparedStatement pstmt = conn.prepareStatement("select * from tab",ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
Set
FetchSize.pstmt.setFetchSize(3);
Execute the statement.
ResultSet rs = pstmt.executeQuery();
Cyclically obtain the next row of data.
while (rs.next()) {}