• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing variables outside while loops  RSS feed

 
Atul Dusara
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);
 
Chris Mathews
Ranch Hand
Posts: 2712
  • 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.
 
narasimharao konjeti
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!