Update语句只运行一次。

时间:2011-10-09 03:18:02

标签: java mysql eclipse jdbc

所以我遇到的问题是第一个查询的结果只会更新一次。

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)。

请帮忙, 谢谢!

2 个答案:

答案 0 :(得分:1)

您必须使用Updatable ResultSet。

答案 1 :(得分:0)

问题是JDBC在尝试同时对这些表进行更改时不允许来自多个表的表查询。