创建帐户JAVA时的验证

时间:2015-05-28 18:46:25

标签: java validation

因此,我的程序的某些部分可以创建一个帐户,并且创建的帐户将被插入到我的数据库中。我正在尝试编写一些代码,其中*引用代码:

public void actionPerformed(ActionEvent e) {
    String user = userField.getText().trim();
    String pass = passField.getText().trim();
    String conPass = confirmPass.getText().trim();  
try{    

    // TODO Auto-generated method stub
    if(e.getSource()==submit){

            if (user.equals(user)&&pass.length()==0){
                JOptionPane.showMessageDialog(null, "Fill in the empty field!");
            }//check if the pass field is blank
            else if(user.length()<5){
            JOptionPane.showMessageDialog(null,"Username must be at least 5 characters!");
            }
            else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
                String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
                getQuery(sqlLogin); 
                JOptionPane.showMessageDialog(null, "Account Successfully Created!");
                create.dispose();
                GUI gui = new GUI();
            }//if(pass.equals(conPass))
            else if(user.length()==0&&pass.length()==0){
                JOptionPane.showMessageDialog(null, "Fill in the empty field!");
            }//check if both fields are blank
            else if (user.length()==0 &&pass.equals(pass)){
                JOptionPane.showMessageDialog(null, "Fill in the empty field!");
            }//check if user field is blank 
            else if(user.equals(user)&&pass!=conPass){
                JOptionPane.showMessageDialog(null, "Password do not match!");
            }//check if password and confirm pass matches
    }

我真的不知道怎么说这个问题,但是看看ifelse if语句,如果用户符合这些条件,程序应该打印JOptionPane的东西。除了第二个,如果。

您可能想知道为什么我将这些代码放在else if

else if(user.equals(user)&&pass.equals(conPass)&&pass.length()!=0){
                String sqlLogin = "insert into tblLogin (username,pssword) values ('"+user+"','"+pass+"')";;
                getQuery(sqlLogin); 
                JOptionPane.showMessageDialog(null, "Account Successfully Created!");
                create.dispose();

原因是,当我尝试将其放入if语句时,我的程序出现了一些逻辑错误。请帮我解决我的代码谢谢:)随意为我编写一个新代码:DD

1 个答案:

答案 0 :(得分:1)

我可能会尝试这样的事情:

public static boolean isSet(String s){
     if(s==null || "".equals(s)) return false;
     return true;
}
//.... your validation here
if(isSet(user) && isSet(pass) && isSet(conPass) && pass.equals(conPass)){
    //create account
}else{
  //smth wrong eg. if(!pass.equals(conPass) { //wrongpass }
}