How do I get only the first row from a ResultSet
? I know how to iterate through the entire set, but how do I get just the first row?
You can move the cursor of the ResultSet object to the first row from the current position, using the first() method of the ResultSet interface. This method returns a boolean value specifying whether the cursor has been moved to the first row successfully.
Invoke the Statement. executeQuery method to obtain the result table from the SELECT statement in a ResultSet object. In a loop, position the cursor using the next method, and retrieve data from each column of the current row of the ResultSet object using getXXX methods.
getString(String columnLabel) Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language. Time. getTime(int columnIndex) Retrieves the value of the designated column in the current row of this ResultSet object as a java.
Instead of iterating over the result set, just check if there exists an entry an read it:
ResultSet r = ...; if(r.next()) { String s = r.getString(1); ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With