这是我的while循环,大的一个,然后我们有一个for循环和一个嵌套的if,如果问题是if中的代码继续循环并使程序崩溃。当我运行我的代码并添加一个现有的id号来验证它时,它会不断地发送错误消息,说明它存在,我无法确定循环的问题在哪里
while (exist>=0){
exist=0;
for(int i=0; i<idArr.length; i++){
if(AddID.getText().equals(idArr[i])){
exist=1;
JOptionPane.showMessageDialog(null, "ID already exists!", "Clinic System", JOptionPane.ERROR_MESSAGE);
AddID.setText("");
addName.setText("");
DOBADD.setText("");
Address.setText("");
PhoneNum.setText("");
GenderAdd.setText("");
break;
}
}
if(exist==0) {
try{
myconn = DriverManager.getConnection(url, user, password);
//mystatShop = myconShop.createStatement();
//myresShop = mystatShop.executeQuery("INSERT INTO patient_test VALUES (?, ?)");
String SQL= "INSERT INTO PATIENT_INFO VALUES (?,?,?,?,?,?)";
PreparedStatement pstate = myconn.prepareStatement(SQL);
pstate.setString(1,AddID.getText());
pstate.setString(2, addName.getText());
pstate.setString(3, DOBADD.getText());
pstate.setString(4, Address.getText());
pstate.setString(5, PhoneNum.getText());
pstate.setString(6, GenderAdd.getText());
pstate.executeUpdate();
JOptionPane.showMessageDialog(null,"Insertion Successful");
AddID.setText("");
addName.setText("");
DOBADD.setText("");
Address.setText("");
GenderAdd.setText("");
PhoneNum.setText("");
myconn.close();
}
catch(Exception e){
System.out.println(e.getMessage());
}
break;
}
}
1条答案
按热度按时间nue99wik1#
while的论点是
while (exist>=0)
所以根据代码,exist值总是等于或大于0。所以循环永远不会结束。像这样放
while (exist>0)
我也不认为有理由使用嵌套循环。只能使用for循环遍历数组。