import java.sql.*;
import java.util.Scanner;
public class KeysetPagingDemo {
private static final String URL =
"jdbc:yugabytedb://localhost:5433/yugabyte";
private static final String USER = "yugabyte";
private static final String PASS = "yugabyte";
private static final int PAGE_SIZE = 10;
public static void main(String[] args) throws Exception {
try (Connection conn = DriverManager.getConnection(URL, USER, PASS);
Scanner scanner = new Scanner(System.in)) {
long lastId = 0;
while (true) {
int rowsFetched = fetchPage(conn, lastId);
if (rowsFetched == 0) {
System.out.println("\n✅ End of result set.");
break;
}
System.out.print("\nPress ENTER for next page...");
scanner.nextLine();
lastId += rowsFetched;
}
}
}
private static int fetchPage(Connection conn, long lastId)
throws SQLException {
String sql =
"SELECT id, payload " +
"FROM demo_paging " +
"WHERE id > ? " +
"ORDER BY id " +
"LIMIT ?";
try (PreparedStatement ps = conn.prepareStatement(sql)) {
ps.setLong(1, lastId);
ps.setInt(2, PAGE_SIZE);
try (ResultSet rs = ps.executeQuery()) {
int count = 0;
while (rs.next()) {
long id = rs.getLong("id");
String payload = rs.getString("payload");
System.out.printf("id=%d payload=%s%n", id, payload);
count++;
}
return count;
}
}
}
}