• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

accessing variables outside while loops  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a recordset, where I take data out of a database through a while loop. i need to access this variable again outside the loop so I can update the database. For some reason, I cannot access this variable again outside the loop.
Has anyone got any ideas?
This is the code.
rs = st.executeQuery("select * from customer where customer_id= '" + session.getAttribute("login") + "'");

while (rs.next())
{
String balance = rs.getString(9);
float result = Float.parseFloat(balance);
//out.println(balance);
int credit = Integer.parseInt(request.getParameter("credits"));
out.println(credit);

float newbalance = result - credit;
out.println(newbalance);
}
out.println(balance);
 
Ranch Hand
Posts: 2713
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is completely appropriate behavior. You have declared the variable balance within your while loop, therefore it goes out of scope when your while loop ends. If you would like to access this outside your while loop then you must also declare it outside your while loop.
This is very basic Java. I recommend you spend some time focusing on Java fundamentals if you are having many problems like these.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there r some variables balance, newbalance,credit inside the while loop, y dont u try to put out side the loop, and try again,,
best of luch..
cheers
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!