So Im having an issue where the results of the first query are only getting updated once.
Statement statement = con1.createStatement();
ResultSet qResult = statement.executeQuery("select e.Grade, e.StudentID, s.StudentID, s.Classification, s.CreditHours, s.GPA"+ " " +
"from Enrollment e, Student s" + " " +
"where e.StudentID = s.StudentID");
double grade = 0.00;
int tCredits = 0;
float newGpa = 0;
String nClass = "";
PreparedStatement statement2;
int rowCount = 0;
String sId = "";
//loop results
while(qResult.next())
{
sId = qResult.getString(2);
//1 parse grade - convert to double and save to variable
grade = parseGrade(qResult.getString(1));
//2 save Tcredit hours to var
tCredits = qResult.getInt(5);
//3 calc new GPA = ((gpa * tCred)+ (3 * grade))/(tCred +3)
newGpa = (float) (((qResult.getDouble(6) * tCredits) + (3 * grade))/(tCredits + 3));
//4 add 3 to Tcredit hours
tCredits = tCredits + 3;
//5 check tCredit hours and update classification
nClass = getNewClass(tCredits);
开发者_如何学Python//6 Update Tables!!
statement = con1.createStatement();
rowCount += statement.executeUpdate("update Student" + " " +
"set Classification=" + "'" + nClass + "'" + ", GPA=" + newGpa +", CreditHours=" + tCredits + " " +
"where StudentID=" + qResult.getString(2));
}
System.out.println("rows Changed:::: " + rowCount);
//statement.close();
}
I am trying to update each result as I traverse the results from my select query. I ahve check and there are multiple students enrolled in multiple classes so students do get returned more than once.
however the credits only get updated 1( +3) for every student that is in the enrollment table.
Please help, thanks!
You have to use Updatable ResultSet.
The issue is that JDBC does not allow table queires from multiple tables while attempting to make changes to those tables, concurrently.
精彩评论