2011-10-09 60 views
0

因此,我有一個問題,第一個查詢的結果只更新一次。更新語句只運行一次。?

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(); 

} 

我試圖更新每個結果,因爲我遍歷了我的選擇查詢的結果。 我要檢查一下,有多個學生在多個班級註冊,所以學生不止一次返回。

但是,學分只會在註冊表中的每個學生更新1(+3)。

請幫忙, 謝謝!

回答

0

問題是,JDBC不允許同時對多個表進行表查詢,同時嘗試對這些表進行更改。

1

您必須使用Updatable ResultSet。

+0

即時通訊仍然有點困惑..所以我要修改是聲明= con1.createStatement(ResultSet.CONCUR_UPDATABLE,ResultSet.TYPE_SCROLL_SENSITIVE); – Aziz