2015-04-07 84 views
1

我得到这个返回结果集的错误。 无法将结果从Resultset转换为双倍executeQuery() - 返回结果集

是不是可以返回一个双?我该怎么办?

public double getBalance(String name) 
{ 
    ResultSet resultSet = null; 

    try 
    { 
     selectBalance.setString(1, name); // specify last name 

     // executeQuery returns ResultSet containing matching entries 
     resultSet = selectBalance.executeQuery(); 


     while (resultSet.next()) 
     { 
     resultSet.getDouble("balance"); 

     } // end while 
    } // end try 
    catch (SQLException sqlException) 
    { 
     sqlException.printStackTrace(); 
    } // end catch 

    return resultSet; 
} 

Thx提前!

+0

什么是查询?桌子是什么?我们需要更多信息 –

回答

1

您应该返回双,如果这就是你需要:

public double getBalance(String name) 
{ 
    double result = 0.0; 
    ResultSet resultSet = null; 

    try 
    { 
     selectBalance.setString(1, name); // specify last name 

     // executeQuery returns ResultSet containing matching entries 
     resultSet = selectBalance.executeQuery(); 


     while (resultSet.next()) 
     { 
     result = resultSet.getDouble("balance"); 

     } // end while 
    } // end try 
    catch (SQLException sqlException) 
    { 
     sqlException.printStackTrace(); 
    } // end catch 

    return result; 
} 

请注意,这将只返回结果集的最后一行读出的值,所以如果你希望返回多行,考虑返回List<Double>

+0

Thx很多。有效 – MDK