5

I am trying to get the first column in the first row of my result set. I know I can change my SQL query to do that. BUT no. I want the full table and I only want to do what I just mentioned.

NOTE - Winners is an Aliased column in my sql query.

The error is basically -

com.microsoft.sqlserver.jdbc.SQLServerException: 
The result set has no current row.

More of the error -

at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.verifyResultSetHasCurrentRow(SQLServerResultSet.java:483)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getterGetColumn(SQLServerResultSet.java:2047)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2082)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getValue(SQLServerResultSet.java:2067)
at com.microsoft.sqlserver.jdbc.SQLServerResultSet.getString(SQLServerResultSet.java:2401) 

This is what I have tried so far and I need your help to fix it -

ResultSet rs = statement.executeQuery("get a whole table"); //pseudocode

try{            
    rs.next();
    numberOne = rs.getString("Winners");
    rs.first(); 
} catch (SQLException e) {
    e.printStackTrace();
}

1 Answer 1

5

There is probably nothing in your result set. Use

if (rs.next()) {
    numberOne = rs.getString("Winners");
}
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, thats strange. How can there be nothing in my result set ? I have executed the same query in my database and I get results. But java fails :(
Either you're not pointing to the same database or schema, or you've not committed the transaction used to insert the rows.
I ran the same query on SQL server which my ResultSet uses. It works fine. Don't know why this does not work in Java. Commit is not a problem, DB is loaded with rows. I only need to read DB and not write to DB.
I used the CachedRowSetImpl along with the irritating ResultSet. Now, it works.
This is completely broken... I have something in my ResultSet but can only use it, when I place rs.beforeFirst(); rs.next(); in front of rs.getSomething();... Any idea why this happens and how to fix it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.