退出While循环

用户3044240

我正在尝试从MySql数据库中读取值,并检查是否需要更新的ID已经存在于数据库中。除了检查数据库部分,我已经能够使程序中的其他所有工作正常进行。这是我的一些代码:

public void updateStatement() throws SQLException{
    try
    {
        connnectDatabse();
    }
    catch (ClassNotFoundException e)
    {
        System.out.println("Could not connect to database..");
    }

    System.out.println("How many entries would you like to update?");
    kb=new Scanner(System.in);
    int numEntries = kb.nextInt();
    int counter =0;
    String newName=null, newDepartment =null;
    int newSalary=0, newId =0;

    int counterValues =0;
    while(counterValues != numEntries){

        System.out.println("Please enter 5 to view current entries in database\n");
        selectStatement();
        int idToUpdate =0;
        boolean idVerify =false;

        //Check if the user id exists in database or not
        while(!(idVerify)){
            System.out.println("\nPlease enter the ID of the record to update");
            idToUpdate = kb.nextInt();
            idFoundInDatabase(idArrayList, idToUpdate);

        }

        System.out.println("Please choose the number of column to update from below options.\n1.ID\n2.Name\n3.Salary\n4.Department");
        int columnToUpdate = kb.nextInt();
        switch(columnToUpdate){
        case 1:
            System.out.println("What will be the new id value for the selected ID?");
            newId = kb.nextInt();
            query = "update employee set ID = ? where ID = ?";

            break;
        case 2:
            System.out.println("What will be the new name for the selected ID?");
            newName = kb.next();
            query = "update employee set Name = ? where ID = ?";
            break;
        case 3:
            System.out.println("What will be the new salary for the selected ID?");
            newSalary = kb.nextInt();
            query = "update employee set Salary = ? where ID = ?";
            break;
        case 4:
            System.out.println("What will be the new department for the selected ID?");
            newDepartment = kb.next();
            query = "update employee set Department = ? where ID = ?";
            break;
        default:
            System.out.println("Correct option not chosen");
        }

        PreparedStatement st = conn.prepareStatement(query);

        if(columnToUpdate ==1){
            st.setInt(1, newId);
            st.setInt(2, idToUpdate);
        }

        else if(columnToUpdate ==2){
            st.setString(1, newName);
            st.setInt(2, idToUpdate);
        }
        else if(columnToUpdate ==3){
            st.setInt(1, newSalary);
            st.setInt(2, idToUpdate);
        }
        else{
            st.setString(1, newDepartment);
            st.setInt(2, idToUpdate);
        }
        //execute the prepared statement
        st.executeUpdate();
        System.out.println("Record successfully updated..");
        counterValues++;
    }
}
   //Code that I am unable to exit. This is 
    a separate method outside of updateStatement() method. 
   ArrayList contains the list of ids that are already in the database. ArrayList has been populated successfully.

 public boolean idFoundInDatabase(ArrayList<String> arrayList, int id){


    boolean validId = false;
    while(validId == true){
        String idRead = String.valueOf(id);
        for (int i=0; i<arrayList.size(); i++){
            String elementRead =arrayList.get(i);
            if(elementRead.equals(idRead)){
                validId = true;
                break;
            }
            else{
                validId= false;
            }
        }
    }
    return validId;
}

}

如果需要的话,我还将代码行发布到获取结果集的位置,以生成ID的数组列表。

 while(result.next()){
            String id =result.getString("ID");
            idArrayList.add(id);
            if(choice == 1 || choice == 2 || choice == 3 || choice == 4){
                resultIs = result.getString(columnName);
                System.out.println(resultIs);                   
            }
            else{
                System.out.println(result.getString("ID")+"\t"+result.getString("Name")+
                        "\t"+result.getString("Salary")+"\t"+result.getString("Department") );
            }
        }

问题正在退出上述idFoundInDatabase方法。它标识用户想要更新的ID是否在数据库中。在找到甚至数据库中存在的正确ID时,它只会继续读取数组列表中的值,而不是返回return语句。在调用此方法的地方我也做错了吗?任何帮助将不胜感激。现在已经坚持了将近一天。已经完成了很多次调试。我这样做只是为了了解jdbc和参数化查询,以便我可以在更大的项目中遵循类似的操作。感谢您的帮助。谢谢你。

帕拉

一些东西:

while内部的循环idFoundInDatabase()不是必需的-通过for循环进行一次简单的迭代arrayList就足够了。

您正在从该函数返回一个布尔值,但是您的调用方法不会捕获并使用它,因此idVerify永远不会将其从更改falsetrue,因此您的输入循环将永远重复。

您要做的所有事情就是将其更改idFoundInDatabase(idArrayList, idToUpdate);idVerify = idFoundInDatabase(idArrayList, idToUpdate);,然后在找到有效ID时,输入循环应成功终止。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章