我正在尝试制作一个Android应用程序,它可以使用数据库中的用户登录,到目前为止它可以获取用户,并且当我可以登录时它会给我一个 toast 。现在它会检查每个用户,因此它会显示 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();
}
}
});
}
最佳答案
更新您的查询,例如..
SELECT * FROM vtiger_users where user=#user# and password=#pass#
关于Java JDBC数据库连接登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27102735/