Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, looping through result set

Tags:

java

jdbc

In Java, I have a query like this:

String querystring1= "SELECT rlink_id, COUNT(*)"                    + "FROM dbo.Locate  "                    + "GROUP BY rlink_id "; 

The table rlink_id has this data:

Sid        lid  3           2  4           4  7           3  9           1 

How do I extract these values with a Java ResultSet?

Here is what I have so far:

String show[] = {rs4.getString(1)}; String actuate[] = {rs4.getString(2)}; asString = Arrays.toString(actuate); 
like image 613
user977830 Avatar asked Oct 04 '11 05:10

user977830


People also ask

How do you iterate through a ResultSet?

Iterating the ResultSet To iterate the ResultSet you use its next() method. The next() method returns true if the ResultSet has a next record, and moves the ResultSet to point to the next record. If there were no more records, next() returns false, and you can no longer.

How can we iterate through database in Java?

Try to split up your logic. This is how you create a list of all shows/seasons from the database. You should then construct ULRs and query the external service using this list, after the resultset (and possibly the connection) is closed.

What is ResultSet in Java?

A ResultSet object is a table of data representing a database result set, which is usually generated by executing a statement that queries the database. For example, the CoffeeTables. viewTable method creates a ResultSet , rs , when it executes the query through the Statement object, stmt .

How do I get all the ResultSet rows?

Getting the number of rows using methodsThe last() method of the ResultSet interface moves the cursor to the last row of the ResultSet and, the getRow() method returns the index/position of the current row.


1 Answers

List<String> sids = new ArrayList<String>(); List<String> lids = new ArrayList<String>();  String query = "SELECT rlink_id, COUNT(*)"              + "FROM dbo.Locate  "              + "GROUP BY rlink_id ";  Statement stmt = yourconnection.createStatement(); try {     ResultSet rs4 = stmt.executeQuery(query);      while (rs4.next()) {         sids.add(rs4.getString(1));         lids.add(rs4.getString(2));     } } finally {     stmt.close(); }  String show[] = sids.toArray(sids.size()); String actuate[] = lids.toArray(lids.size()); 
like image 117
Maurice Perry Avatar answered Sep 19 '22 17:09

Maurice Perry