1

我在返回结果集中收到此错误。 无法将结果集的结果转换为 double

是不是可以返回双倍?我应该怎么办?

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;
} 

提前谢谢!

4

1 回答 1

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;
} 

请注意,这只会返回从结果集的最后一行读取的值,因此如果您希望返回多行,请考虑返回 a List<Double>

于 2015-04-07T19:34:47.807 回答