Bug #23337 get java.sql.SQLException: Before start of result set
Submitted: 16 Oct 2006 15:00 Modified: 16 Oct 2006 17:16
Reporter: Sasi Levi Email Updates:
Status: Not a Bug Impact on me:
None 
Category:Connector / J Severity:S1 (Critical)
Version: OS:
Assigned to: CPU Architecture:Any

[16 Oct 2006 15:00] Sasi Levi
Description:
I run the following code : 
con =  ConnectionAgentImpl.getInstance().openConnection();
pstmt = (PreparedStatement) con.prepareStatement("my sql string");
ResultSet rs = (ResultSet) pstmt.executeQuery();

//initilazing the p
while (rs.next())
{
   maxId = pstmt.executeQuery().getInt("id");
}

The table return only one value so I don't need the while statment but in any ways that I tried to get the value I get java.sql.SQLException: Before start of result set.

Fourms didn't helped much! What to do? I'm using the implemention of the driver.

How to repeat:
1. make connection to MYSQL.
2. make PreparedStatement of mysql driver (not java.sql).
3. run executeQuery and get the resultset.
4. try to get the value.
[16 Oct 2006 15:22] Mark Matthews
Executing a statement closes any open result sets that statement has created. Since you're re-executing the statement in your while{}, this code can not work.
[16 Oct 2006 17:16] Sasi Levi
You right I didn't paste the right code, my fault , 
if (rs.last())
{
   maxId = rs.getInt(1);
}

The first code was pstmt.executeQuery().getInt(1) and on that was the error.
[16 Oct 2006 17:24] Mark Matthews
You can't do "pstmt.executeQuery().getInt(1)" either. In JDBC newly-created result sets have their "cursor" positioned before the start of the result set. You can' call any get*() methods until you navigate into the result set somehow (e.g. first(), last(), next(), absolute(), relative(), etc. and check the result, to make sure you're on a valid row).