it is because "TotalSale" is defined inside a while loop and inside if
statement in your second example. Try this:

float TotalSale = 0; //moved declaration outside the while loop
while (Rs.next()) {
    TotalSale = Rs.getFloat("n_totalprice");
}
//Should be no error
System.out.println(TotalSale);

On Jun 15, 11:27 am, marc <[email protected]> wrote:
> Hi all
>
> I am getting an error on the following code when I want to access the
> variable "TotalSale" out side the loop.
> ***************************************************************************
>  ResultSet Rs = sta.executeQuery("SELECT n_totalprice,id FROM orders
> WHERE  ID = IDENT_CURRENT('orders')");
>
>                      while (Rs.next()) {
>                      float TotalSale = Rs.getFloat("n_totalprice");
>                      System.out.println(TotalSale);// no error
>                       }
>  System.out.println(TotalSale);//Error TotalSale cannot be resolved
> ***************************************************************************
>
> I am getting single value from the databse so i also tried with the
> following code but still the result is same.
>
> ***************************************************************************
>   if (Rs.next()){float TotalSale = Rs.getFloat("n_totalprice");}
> ***************************************************************************
>
> Can any body please help me.Thank you in advance

-- 
You received this message because you are subscribed to the Google Groups "The 
Java Posse" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to