wrote:
> Hi,
>
> I have written a function as follows
>
> public String fetchName(String query) throws Exception
> {
>
> stmt = con.createStatement();
> ResultSet rs = stmt.executeQuery(query);
> rs.next();
> return (rs.getString(1));
> }
>
> If i don't write the statement,
> rs.next and directly use rs.getString(1), i get exception
>
> Why is it so, why do we need to move one record next???
>
> Thanks
>
I'm not entirely sure why it is. I'd imagine its a pointer
which starts before the first tuple in your result set.
Initially
-->
Row1
Row2
Row3
....
Calling rs.next() moves the pointer down.
....
--> Row1
Row2
....
But it does allow you to do
while(rs.next()){
//get elements
}
because rs.next() returns true when there is
another row and false if there was no next row.
Chris