我想防止我的Java程序在数据库中接受/插入空白或“”值。我已经将用户名和密码列设置为不为null,但是它继续接受空白或“”值。
这是我向数据库添加值的代码的一部分:
private void btnAddActionPerformed(java.awt.event.ActionEvent evt) {
try{
theQuery("insert into accounts (username,password) values ('"+user1.getText()+"', '"+pass1.getText()+"')");
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "Unable to add record to the database");
}
}
PS密码不会被散列,因为这仅用于活动。
尝试这样:
String str=user1.getText();
if ("".equals(str)){ //User have not entered anything.
JOptionPane.showMessageDialog(null,"Please enter your name.");
nameField.requestFocusInWindow();
//Do NOT loop here.
}
else {
//Do everything you need to do when the user have entered something
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句