Skip to content

Instantly share code, notes, and snippets.

@sulmansarwar
Created February 9, 2017 05:57
Show Gist options
  • Save sulmansarwar/02ee4aa30da951efea57bf339f6cd78d to your computer and use it in GitHub Desktop.
Save sulmansarwar/02ee4aa30da951efea57bf339f6cd78d to your computer and use it in GitHub Desktop.
1- Retrieve data using JDBC 2- Populate java.util.List from java.sql.ResultSet 3- Paginate java.util.List
String query = "SELECT DISTINCT article_id from articles WHERE article_summary IS NULL";
SqlConnection connection = new SqlConnection();
SqlCommand command = new SqlCommand(query, connection.getDatabaseConnection());
ResultSet resultSet = command.executeSelect();
int counter = 1;
List idList = new ArrayList();
//populate List::idList
while (resultSet.next()) {
idList.add(resultSet.getInt("article_id"));
}
int start = 0;
int end = 0;
int pageSize = 10;
do {
start = end;
if ((idList.size() - start) < pageSize) {
pageSize = (idList.size() - start);
}
end = start + pageSize;
List subList = idList.subList(start, end);
System.out.println(counter++ + " - " + subList);
} while (((idList.size() - start) > pageSize));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment