Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

sql

jdbc

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();
}
like image 549
Apple Grinder Avatar asked May 28 '13 21:05

Apple Grinder


1 Answers

There is probably nothing in your result set. Use

if (rs.next()) {
    numberOne = rs.getString("Winners");
}
like image 137
JB Nizet Avatar answered Oct 13 '22 17:10

JB Nizet