Trying to use a rs and a executeUpdate in same method

Essentially I am trying to take a piece of data, add to that data, then update the database with the new value. I am also trying to apply it across multiple rows. When I run the program it iterates once. in trouble shooting I definitely narrowed it down to a conflict I dont completely understand. I get the following error statement, but I havent closed the rs in the code. If I take out the executeUpdate() I can print out all the rows that would otherwise be affected, but I cant run my update.
"Problem with training program: java.sql.SQLException: Operation not allowed after ResultSet closed"
suggestions? I modified the switch conditional for simplicity sake.
try {
Class.forName(driverName);
conn = DriverManager.getConnection(url, userName, password);
state = conn.createStatement();
rs2 = null;
//========================= rs1 =================================================>>>>>>>>
// Reads in individual teams win and losses.
rs = state.executeQuery("SELECT * FROM " + table + " WHERE (training > 0 AND training < 10) AND Team_ID !=0");
while ( rs.next() ) {
PlayerID = rs.getInt("Player_ID");
TeamID = rs.getInt("Team_ID");
Age = rs.getInt("age");
Training = rs.getInt("training");
String skill = "";
float improveF = 0;
if (TeamID == 0) {
improvement = 0;
System.out.println(PlayerID + " has no team, skipped training.");
} else {
// checks if player is over 30 (no improvment)
if (Age >= 30){
improvement = 0;
System.out.println(PlayerID + " is over 30, skipped training.");
} else {
if (Training == 16 || Training == 0) {
improvement = 0;
System.out.println(PlayerID + " is not assigned to practice.");
} else {
switch(Training){
//Speed
case 1:
skillValue = rs.getFloat("SPEED");
maxSkillValue = rs.getFloat("max_SPEED");
skill = "SPEED";
break;
// Stamina
} // end switch
System.out.println(skill);
if (skillValue >= maxSkillValue) {
improvement = 0;
System.out.println(PlayerID + "Skills are maxed in " + skill);
} else {
improveF = (float) .015;
improvement = (test.trainingImprovement() * improveF);
skillValue += improvement;
System.out.println(improvement);
System.out.println(skillValue);
state.executeUpdate("UPDATE " + table +
" SET " + skill + "= '" + skillValue +
"', improve=" + improvement +
" WHERE Player_ID = '" + PlayerID + "'" );
} // end No Training Setting
} // end age IF test
} // end TeamID IF test
} // end while
conn.close();
} catch (Exception e) {
System.out.println("Problem with training program: " + e.toString() );
finally {
System.out.println("Player Training Executed");
} // END Finally

Any additional suggestions would be great but I got my code to work.
I changed the state.executeUpdate() to state2.executeUpdate. I did some reading in the forums and it was suggested that i cant use the my initial rs.

Similar Messages

Maybe you are looking for