简体   繁体   中英

Working with a ResultSet

I am trying to get a count from a particular table in my derby database, but when I run the code I keep getting Invalid operation at current cursor position

    public int getNumUsers(){
    ResultSet rs = null;
    int size = -1;
    try {
        stmt = conn.createStatement();
        rs = stmt.executeQuery("select count(*) from USERS");
        while(rs.next()){
            size = rs.getInt("COUNT");
        }
        stmt.close();
    } catch (SQLException sqe) {
        sqe.printStackTrace();
    }
    return size;
}

Change your query to

select count(*) As COUNT from USERS

or change your function call to

rs.getInt(1);

give an alias to count(*) in your select statement . in mysql we use as to give alias name.I dunno about derby though, but think it'd be similar.

rs = stmt.executeQuery("select count(*) as count from USERS");
        while(rs.next()){
            size = rs.getInt("count");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM