我正在尝试制作一个 android 应用程序,该应用程序可以从数据库中与用户一起登录,到目前为止,它可以获取用户,当我可以登录时,它会给我一个吐司。 现在它会检查每个用户,因此它为每个用户显示 toast,例如,当我输入有效的用户名和密码时,它会首先说错误的用户名或密码,登录后......我知道这是因为 while 循环,但我不知道如何在用户名和密码错误时显示一次。这是我的代码
private void setupMessageButton() {
button = (Button) findViewById(R.id.button1);
text = (TextView) findViewById(R.id.editText1);
text2 = (TextView) findViewById(R.id.editText2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textje = text.getText().toString();
textje2 = text2.getText().toString();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://dbhost", "root", "dbpass");
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM vtiger_users");
while(rs.next()) {
users = rs.getObject(2).toString();
wachtwoorden = rs.getObject(3).toString();
if(textje.equals(users) && textje2.equals(wachtwoorden)) {
Toast.makeText(getApplicationContext(), "Logging in..", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Wrong username or password", Toast.LENGTH_SHORT).show();
}
}
con.close();
} catch(Exception e) {
e.printStackTrace();
}
}
});
}
更新您的查询,例如..
从用户=#用户#和密码=#传递#的vtiger_users中选择 *