2012-09-10 76 views
1

我在Android中开发了一个登录表单。我在这里使用了验证。我必须填写任何人(用户名或密码),那么我的应用程序应该显示成功!,并应该转移到其他活动。在android中登录表单验证

但是,如果两个字段都为空,则不应显示成功消息,并且应该显示登录失败!

请帮我这个。

这是我的web服务代码:

public class XcartLogin { 
    public String authentication(String userName, String password) { 
     String retrievedUserName = ""; 
     String retrievedPassword = ""; 
     String status = ""; 
     try { 
      Class.forName("com.mysql.jdbc.Driver"); 
      Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/xcart432-pro", "root", ""); 
      PreparedStatement statement = con.prepareStatement("SELECT * FROM xcart_customers WHERE login = '" + userName + "'"); 
      ResultSet result = statement.executeQuery(); 
      while (result.next()) { 
       retrievedUserName = result.getString("login"); 
       retrievedPassword = result.getString("password"); 
      } 
      if (retrievedUserName.equals(userName) && retrievedPassword.equals(password)) { 
       status = "Success!"; 
      } else { 
       status = "Login fail!!!"; 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return status; 
    } 
} 

这是验证了我的Android代码:

if(status.equals("Success!")) 
    { 
     // ADD to save and read next time 
     String strUserName = userName.getText().toString().trim(); 
     String strPassword = userPassword.getText().toString().trim(); 
     if (null == strUserName || strUserName.length() == 0) 
     { 
      // showToast("Enter Your Name"); 
      userName.setError("username is required!"); 
      isUserValidated = false; 
     } 
     if (null == strPassword || strPassword.length() == 0) 
     { 
      // showToast("Enter Your Password"); 
      isPasswordValidated = false; 
      userPassword.setError("password is required!"); 
     } 
    } 
+0

刚刚脱离主题..上面的代码形式非常容易进行SQL注入,您不应该从数据库中取回密码,而应将其作为参数传递给SQL准备语句 – Harish

回答

2

尝试使用此条件:

if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)&&!(retrievedUserName.equals("") && retrievedPassword.equals(""))) 

,而不是你的条件:所有的

if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)) 
+0

亚这是工作..谢谢你。 –

+0

欢迎编码 –

+0

欢迎,甚至Sujay建议更改准备好的声明。 你的是一个准备好的声明的错误用法 –

0

首先,你使用PreparedStatement的方式不正确。这里是你应该如何修改它:

PreparedStatement statement = con.prepareStatement("SELECT * FROM xcart_customers WHERE login = ?"); 

你会再使用setString(int paramIndex, String value)设置的值,然后调用executeQuery()方法。像这样:

PreparedStatement statement = con.prepareStatement("SELECT * FROM xcart_customers WHERE login = ?"); 
statement.setString(1, userName); 

ResultSet result = statement.executeQuery(); 

这是安全的,并且在代码中使用PreparedStatement的实际方法。现在

,以测试你的要求,你应该做这样的事情:

if(userName.equals(retrievedUserName)&&password.equals(retrievedPassword)&&!("".equals(retrievedUserName) && "".equals(retrievedPassword))) 

请注意,我检查的userName密码retrievedUserNameretrievedPassword因为有retrieveUserNameretrievePassword可能为JavadocgetString(int)为空。

返回:列值;如果该值为SQL NULL,则返回值 为空

如果值是前来为空,那么你会处理一个NullPointerException,我猜你可能希望避免的。另外,出于同样的原因,您甚至可以在尝试查询数据库之前检查空值的参数。

2

在您的验证码if(status.equals("Success!"))声明之前,你应该这样做,首先要避免查询数据库,如果任何文本字段都是摆在首位空:

boolean errorOccurred = false; 
if (strUserName.equals("")) { 
    userName.setError("Username is required!"); 
    errorOccurred = true; 
} 

if (strPassword.equals("")) { 
    userName.setError("Password is required!"); 
    errorOccurred = true; 
} 

if (errorOccurred) { 
    return; // avoids executing the part of your code which queries the db 
} 

检查是否输入字段值是null是相当无意义的,因为如果它们不包含任何内容,它将只是一个空字符串,或""。然后,为了简化您的webservice代码...

if (result.next()) { // use if instead of while, because ideally, only ONE record should 
        // be returned and hence, no need to loop; 

    // then, just get the corresponding password 
    retrievedPassword = result.getString("password"); 
} 

if (retrievedPassword.equals(password)) { 
    status = "Success!"; 
} 

进一步的建议:把“成功!”在String常量中,并使用该值代替文字值。您以这种方式犯错误的可能性较小,并且可以更轻松地编辑您的代码。