This article gives an exampleJDBC Using cursors to implement paging queries. Share with you for your reference, As follows:
/** * Only query from database at a timemaxCount Bar record * @param sql Afferentsql Sentence * @param startNo From which record
* @param maxCount How many records in total */ public void getData(String sql,int startNo,int
maxCount){ Connection conn = ConnectionUtil.getConnection(); try { //
conn.prepareStatement(sql, cursor type, Can records be updated); // cursor type: //
ResultSet.TYPE_FORWORD_ONLY: forward-only cursor //
ResultSet.TYPE_SCROLL_INSENSITIVE: Scrolling. But not affected by other users' changes to the database. //
ResultSet.TYPE_SCROLL_SENSITIVE: Scrolling. This record changes when other users change the database. // Can records be updated: //
ResultSet.CONCUR_READ_ONLY, read-only // ResultSet.CONCUR_UPDATABLE, Renewable
PreparedStatement pstat =
conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
// Maximum number of records found pstat.setMaxRows(startNo+maxCount-1); ResultSet rs =
pstat.executeQuery(); // Move cursor to first record rs.first(); // Cursor moves to the first record to output
rs.relative(startNo-2); while(rs.next()) System.out.println(rs.getInt(1)); }
catch (SQLException e) { e.printStackTrace(); } } /** *
Query all records from database, Then get the required value through the cursormaxCount Bar record * @param sql Afferentsql Sentence * @param startNo
From which record * @param maxCount How many records in total */ public void getDataFromAll(String
sql,int startNo,int maxCount){ Connection conn =
ConnectionUtil.getConnection(); try { PreparedStatement pstat =
conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
ResultSet rs = pstat.executeQuery(); rs.first(); rs.relative(startNo-1); int i
= startNo-1; while(i < startNo + maxCount-1 && !rs.isAfterLast()){
System.out.println(rs.getInt(1)); i++; rs.next(); } } catch (SQLException e) {
e.printStackTrace(); } }