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
Ingo R. Homann - 19 Apr 2006 14:51 GMT
Hi,
> I have written a function as follows
>
[quoted text clipped - 11 lines]
>
> Why is it so, why do we need to move one record next???
Because
(1) There may be more than one record in the result set.
(2) There may be no record in the result set.
You must be able to handle that!
Ciao,
Ingo
Chris Diver - 19 Apr 2006 14:59 GMT
> Hi,
>
[quoted text clipped - 15 lines]
>
> 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