btnLogin = (Button)findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new View.OnClickListener(){ public void onClick(View v){ sql = "select * from LoginDB"; boolean validLogin = loginCheck(sql, etUser.getText.toString() ,etPw.getText.toString()); if(validLogin==true) { Toast.makeText(getApplicationContext(), "Login success",Toast.LENGTH_LONG).show(); } else { Toast.makeText(getApplicationContext(), "Wrong Password ",Toast.LENGTH_LONG).show(); } } });
boolean loginCheck(String sql , String etUser,String etPassword ) { boolean validLogin= false; try{ System.out.println(sql); Cursor cursor = db.rawQuery(sql,null); while (cursor.moveToNext()) { // sql="create table if not exists LoginDB ( username text, password text)"; String userNameDB = cursor.getString(cursor.getColumnIndex("username")); String pwDB = cursor.getString(cursor.getColumnIndex("password")); if (userNameDB.equals(etUser)&&pwDB.equals(etPassword)) { validLogin = true; break; //break out of while loop } } } catch (Exception e) { System.out.println(e.toString()); return validLogin; } return validLogin; }