1

I am getting this error in return resultset. cannot convert results from Resultset to double.

Isn't it possible to return a double? What should i do?

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 in advance!

MDK
  • 95
  • 1
  • 1
  • 8

1 Answers1

1

You should return a double if that's what you need :

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

Note that this would only return the value read from the last row of the result set, so if you expect multiple rows to be returned, consider returning a List<Double>.

Eran
  • 387,369
  • 54
  • 702
  • 768