我在Android中开发了一个登录表单。我在这里使用了验证。我必须填写任何人(用户名或密码),然后我的应用程序应显示成功!并应转移到其他活动。
但是,如果两个字段都为空,则不应显示成功消息,并且应显示Login failed !!!。
请帮帮我。
这是我的网络服务代码:
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!" );
}
}
尝试使用这个条件:
if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password)&&!(retrievedUserName.equals("") && retrievedPassword.equals("")))
而不是你的条件:
if(retrievedUserName.equals(userName)&&retrievedPassword.equals(password))
在验证代码中的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
是非常没有意义的,因为如果它们不包含任何内容,它将只是一个空字符串,或""
。然后,简化您的Web服务代码......
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
常量中使用而不是字面值。通过这种方式犯错的可能性较小,并且可以更轻松地编辑代码。
首先,您使用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)))
请注意,我正在检查用户名和密码来检索retrieveUserName和retrievePassword,而不是相反,因为根据Javadoc的getString(int)
,retrieveUserName或retrievePassword可能为null。
返回:列值;如果值为SQL NULL,则返回的值为null
如果该值为null,那么你将处理一个NullPointerException
,我想你可能想要避免。此外,出于同样的原因,您可能希望在尝试查询数据库之前检查null值的参数。