Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get only the first row from a ResultSet

Tags:

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?

like image 544
Sanja Avatar asked Apr 27 '11 07:04

Sanja


People also ask

How do you get the first row in a ResultSet?

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.

How do you retrieve values in a ResultSet?

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.

What is ResultSet getString ()?

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.


1 Answers

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);   ... } 
like image 147
hage Avatar answered Sep 24 '22 15:09

hage