Looping Through Java Result Sets
In Java, a ResultSet provides a tabular view of database query results. Iterating through the results is a common operation. Let's consider the following example:
You have a query that groups rows based on the "rlink_id" column and counts the occurrences of each unique value:
String querystring1 = "SELECT rlink_id, COUNT(*)" + "FROM dbo.Locate " + "GROUP BY rlink_id ";
The corresponding "rlink_id" table contains the following data:
Sid lid 3 2 4 4 7 3 9 1
To loop through the results of this query using a ResultSet, you can utilize the following steps:
List<String> sids = new ArrayList<>(); List<String> lids = new ArrayList<>();
Statement stmt = yourconnection.createStatement(); ResultSet rs4 = stmt.executeQuery(query);
while (rs4.next()) { sids.add(rs4.getString(1)); lids.add(rs4.getString(2)); }
stmt.close();
String show[] = sids.toArray(sids.size()); String actuate[] = lids.toArray(lids.size());
The above is the detailed content of How Do I Iterate Through a Java ResultSet to Extract Data from a Grouped Query?. For more information, please follow other related articles on the PHP Chinese website!