Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Resultset in Java Program

Resultset rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");

Using the above java code above, am retrieving the counts of rows from the table named feedsCA.

While trying to retrieving the counts using rs.getInt(1),rs.getInt(2),rs.getInt(3), I end with an error saying as below,

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The result set has no current row.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetHasCurrentRow(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
    at SimpleMail.main(SimpleMail.java:151)

UPDATE:

The above exception has been resolved.

But I get the following exception, for which I dont know the reason. Please advise.

Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The index 2 is out of range.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyValidColumnIndex(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(Unknown Source)
    at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getInt(Unknown Source)
    at SimpleMail.main(SimpleMail.java:152)

This is how I have updated my program. Find me a logical way as I can understand well that the loop below will not work as required.

rs=stmt.executeQuery("select count(*) from feedsca group by score order by score");
while(rs.next()){
pw.printf(rowFormat, rs.getLong(1),"0",rs.getLong(2),rs.getLong(3));}
like image 256
LGAP Avatar asked Oct 25 '10 16:10

LGAP


People also ask

What is the use of ResultSet in Java?

A ResultSet is a Java object that contains the results of executing an SQL query. In other words, it contains the rows that satisfy the conditions of the query. The data stored in a ResultSet object is retrieved through a set of get methods that allows access to the various columns of the current row.

Why do we use ResultSet in JDBC?

JDBC ResultSet interface is used to store the data from the database and use it in our Java Program. We can also use ResultSet to update the data using updateXXX() methods. ResultSet object points the cursor at before the first row of the result data. Using the next() method, we can iterate through the ResultSet.


3 Answers

You have to move the cursor of the result set to a row - either by resultSet.first() or by resultSet.next(). Initially the cursor is pointing before the first row, hence your exception.

When you want to iterate the ResultSet:

while(rs.next()) {
    ...
}

Update: For your second problem - (as noted by Casablanca) your query seems to return only one column, and you are asking for a 2nd and 3rd - and they are not found. Note that in rs.getX(idx) idx is the column, not the row.

like image 71
Bozho Avatar answered Oct 08 '22 01:10

Bozho


You need to call rs.next() before accessing the first row.

Typically, you will iterate over the result set like this:

ResultSet rs = ...;
while (rs.next()) {
  ...
}

Update: Note that SELECT COUNT(*) ... returns only one field per row, which is the count. You may have several rows, but each row will have only one field, which has index 1. You need to iterate through the rows to get all the counts:

while (rs.next()) {
  System.out.println(rs.getInt(1));
}

Yet another update: It's bad to assume that your query will always return only 3 rows. However, if you are absolutely sure of this, then you can just call next 3 times manually:

long l1, l2, l3;
rs.next();
l1 = rs.getLong(1);
rs.next();
l2 = rs.getLong(1);
rs.next();
l3 = rs.getLong(1);
pw.printf(rowFormat, l1,"0",l2,l3);
like image 39
casablanca Avatar answered Oct 08 '22 02:10

casablanca


You need to use one of the methods to move the ResultSet cursor to a row before using the getxxx methods. i.e. rs.next(), rs.first() or rs.last(). These methods return true if a valid row has been located so a typical pattern is

if (rs.first()) {
  int field1 = rs.getInt(1); 
  // other columns
}

or for a query that returns multiple rows:

while (rs.next()) {
  int field1 = rs.getInt(1);
  // other columns
}
like image 23
mikej Avatar answered Oct 08 '22 01:10

mikej