Java foreach循环中的For循环

Java foreach循环中的For循环,java,database,postgresql,for-loop,Java,Database,Postgresql,For Loop,所以我有这个代码段。searchStudentWith2RUCode()返回学生对象的数组列表。学生对象具有各种属性,如名字、姓氏、cprnumber、gpa等 public void assignStudents() throws ClassNotFoundException, SQLException { ArrayList<Student> notYetAssignedStudents = searchStudentsWith2RUCode(); Arra

所以我有这个代码段。searchStudentWith2RUCode()返回学生对象的数组列表。学生对象具有各种属性,如名字、姓氏、cprnumber、gpa等

 public void assignStudents() throws ClassNotFoundException, SQLException {
     ArrayList<Student> notYetAssignedStudents = searchStudentsWith2RUCode();
     ArrayList<Student> assignedStudents = new ArrayList<>()
          for (Education edu : eduList) {

               for (int i = 0; i < edu.getAmountOfFreeSpacesInEdu(); i++) {
                  assignedStudents.add(notAssignedStudents.get(i));                      
                  setAssignedStudentsAdmitted(assignedStudents.get(i).getCPR, assignedStudents.get(i).getPrio, assignedStudents.get(i).eduNumber)
        }
    }
}
}

这是我的dbExecuteUpdate方法,它在setAssignedStudentsCommitted中调用

    public static void dbExecuteUpdate(String sqlStmt) throws SQLException, ClassNotFoundException {
    //Declare statement as null
    Statement stmt = null;
    try {
        //Connect to DB (Establish Postgres Connection)
        databaseConnect();
        //Create Statement
        stmt = conn.createStatement();
        //Run executeUpdate operation with given sql statement
        stmt.executeUpdate(sqlStmt);
    } catch (SQLException e) {
        System.out.println("Problem occurred at executeUpdate operation : " + e);
        throw e;
    } finally {
        if (stmt != null) {
            //Close statement
            stmt.close();
        }
        //Close connection
        databaseDisconnect();
    }
}

它成功构建,但不会更改数据库中的任何内容。

可能是您将自动提交设置为false?确保在databaseConnect()上,或为了快速测试,在最后执行提交

//Declare a UPDATE statement
    String updateStmt
            = "UPDATE Students SET sagsbehandling='Optaget' WHERE cprnr ='" + CPR + "' AND prio='" + prio + "' AND eduNumber='" + eduNumber + "';";
范围
updateStmt
setAssignedStudentsCommitted
方法中。那么,如何在下面的方法调用中传递它呢

 public static void dbExecuteUpdate(String sqlStmt) 
         throws SQLException, ClassNotFoundException {

您的第一段代码无法编译。您至少缺少了一个分号,如果您也正确缩进代码,那么读取代码会更容易。接下来,您应该隔离代码的哪一部分实际上是有问题的。
dbExecuteUpdate
是否独立工作?接下来,停止像那样构建SQL—改用参数化SQL。您当前的代码容易受到SQL注入攻击(以及其他问题)。您的提交在哪里?复制我的代码时,我可能丢失了一些缩进和分号。一切都在netbeans中编译。dbExecuteUpdate工作正常。我想象在两个循环中工作是我的问题。我将改为参数化sql,在添加到列表时调用db似乎不是一个好的做法,我将在for循环之外进行
 public static void dbExecuteUpdate(String sqlStmt) 
         throws SQLException, ClassNotFoundException {