Do I need next() for a ResultSet with one row?

3.3k views Asked by At

I have the following code

Statement stmt = SqlHelper.initializeDB();
String query = "SELECT status " + 
                "FROM books " + 
                "WHERE bookId = '" + bookId + "'";
ResultSet rs = stmt.executeQuery(query);
result = rs.getString(1);
rs.close();
SqlHelper.closeConnection();

Do I need to use rs.next()? I am sure there is only going to be one row of data because bookId must be unique in the table. But by default, the cursor of ResultSet starts before the first row so I'm not sure if I need next() or not.

1

There are 1 answers

0
Jigar Joshi On BEST ANSWER

Yes you need next()

A ResultSet object maintains a cursor pointing to its current row of data. Initially the cursor is positioned before the first row...