com.microsoft.sqlserver.jdbc.SQLServerException: T

2019-05-02 05:03发布

问题:

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:

There is probably nothing in your result set. Use

if (rs.next()) {
    numberOne = rs.getString("Winners");
}


标签: java sql jdbc