所以我遇到的问题是第一个查询的结果只会更新一次。
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);
//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();
}
我试图在遍历select查询的结果时更新每个结果。 我检查过,有多个学生报名参加多个班级,所以学生不止一次回来。
但是,对于注册表中的每个学生,学分只会更新1(+3)。
请帮忙, 谢谢!