退出 While 循环
Exiting While Loop
我正在尝试从 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 和参数化查询,以便我可以在更大的 project.Any 帮助中关注类似的事情。谢谢。
几件事:
您在 idFoundInDatabase()
内的 while
循环不是必需的 - for
循环对 arrayList
进行一次简单的迭代就足够了。
您正在从该函数返回一个布尔值,但您的调用方法不会捕获和使用它,因此 idVerify
永远不会从 false
更改为 true
,因此你的输入循环永远重复。
您需要做的就是将 idFoundInDatabase(idArrayList, idToUpdate);
更改为 idVerify = idFoundInDatabase(idArrayList, idToUpdate);
,然后您的输入循环应该会在找到有效 ID 时成功终止。
我正在尝试从 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 和参数化查询,以便我可以在更大的 project.Any 帮助中关注类似的事情。谢谢。
几件事:
您在 idFoundInDatabase()
内的 while
循环不是必需的 - for
循环对 arrayList
进行一次简单的迭代就足够了。
您正在从该函数返回一个布尔值,但您的调用方法不会捕获和使用它,因此 idVerify
永远不会从 false
更改为 true
,因此你的输入循环永远重复。
您需要做的就是将 idFoundInDatabase(idArrayList, idToUpdate);
更改为 idVerify = idFoundInDatabase(idArrayList, idToUpdate);
,然后您的输入循环应该会在找到有效 ID 时成功终止。