我有一個問題,我有一個系統來添加用戶,我必須檢查,如果這個用戶之前存在,這樣我不會再添加他,我從數據庫檢索所有名稱到arraylist.I首先檢查如果數組列表是空的,這樣我可以添加用戶否則,如果他的存在與否 這裏是代碼,他將檢查插入數據庫
if(names.size() == 0){
dbstatement.executeUpdate("
insert into users (user_name,user_password,user_type)
values ('" + username + "','" + userpassword + "','" + type + "')");
JOptionPane.showMessageDialog(rootPane, "user added successfully");
}
else{
for (int i = 0; i < names.size(); i++) {
if (username.equals(names.get(i))) {
JOptionPane.showMessageDialog(rootPane, "Sorry, this name already exist");
break;
}
}
dbstatement.executeUpdate
("insert into users(user_name,user_password,user_type)
values ('" + username + "','" + userpassword + "','" + type + "')");
}
的問題是,當該程序發現了一個名字存在之前,他告訴我並添加他,我知道這個問題的原因,我想知道在哪裏,但如果在裏面的for循環,我想他告訴我用戶存在只是不要再添加它
我知道你沒有指定這個,但考慮你是否需要['upsert'](http://en.wikipedia.org/wiki/Upsert)。 – onedaywhen