Using Resultset in Java Program

后端 未结 4 2105
夕颜
夕颜 2021-01-05 01:18
Resultset rs=stmt.executeQuery(\"select count(*) from feedsca group by score order by score\");

Using the above java code above, am retrieving the

4条回答
  •  情歌与酒
    2021-01-05 01:45

    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);
    

提交回复
热议问题