This article describes the method of JDBC using cursors to implement paging query. Share it for your reference, as follows:
/*** Only query the maximum maxCount record from the database at a time* @param sql The sql statement passed in * @param startNo Which record starts from * @param maxCount How many records are taken in total*/public void getData(String sql,int startNo,int maxCount){ Connection conn = ConnectionUtil.getConnection(); try {// conn.prepareStatement(sql, cursor type, can the record be updated);// Cursor type:// ResultSet.TYPE_FORWORD_ONLY: Only enter the cursor// ResultSet.TYPE_SCROLL_INSENSITIVE: Scrollable. But it is not affected by other users' changes to the database. // ResultSet.TYPE_SCROLL_SENSITIVE: Scrollable. This record will also change when other users change the database. // Can the record be updated: // ResultSet.CONCUR_READ_ONLY, read-only // ResultSet.CONCUR_UPDATABLE, can update PreparedStatement pstat = conn.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY); //The maximum query of which record is pstat.setMaxRows(startNo+maxCount-1); ResultSet rs = pstat.executeQuery(); //Move the cursor to the first record rs.first();// The cursor moves to the first record to be output rs.relative(startNo-2); while(rs.next()) System.out.println(rs.getInt(1)); } catch (SQLException e) { e.printStackTrace(); }}/*** Query all records from the database, and then use the cursor to get the required maxCount records* @param sql statement passed in * @param startNo Which record starts from * @param maxCount How many records are taken 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(); }}For more information about Java related content, please check out the topics of this site: "Java Data Structure and Algorithm Tutorial", "Summary of Java File and Directory Operation Skills", "Summary of Java Operation DOM Node Skills" and "Summary of Java Cache Operation Skills"
I hope this article will be helpful to everyone's Java programming.