Android:如何创建从数据库进行身份验证的登录页面

时间:2011-02-28 08:42:53

标签: android database android-widget

我有3个java文件 我)DBAdapter.java ii)signup.java iii)login.java 四个是菜单,但没有必要在这里提及。

现在,当用户进入登录页面并在editview中输入用户名和密码时,我想这样做,检查数据库中的数据并在找到匹配项时重定向到菜单。 我不知道怎么做,这是我的DBAdapter.java和login.java的代码文件

DBAdapter.java

public class DBAdapter 
{
    public static final String KEY_ROWID = "_id";
    public static final String KEY_FIRSTNAME = "FirstName";
    public static final String KEY_LASTNAME = "LastName";
    public static final String KEY_USERNAME = "UserName";
    public static final String KEY_PASSWORD = "Password";    
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "master";
    private static final String DATABASE_TABLE = "register";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE =
        "create table titles (_id integer primary key autoincrement, "
        + "FirstName text not null, LastName text not null, " 
        + "UserName text not null, Password text not null);";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }

login.java

public class login extends Activity {

    private EditText username;
    private EditText password;

    public String user_name;
    public String pass_word;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        username = (EditText) findViewById(R.id.ev_unameLogin);
        password = (EditText) findViewById(R.id.ev_passwordLogin);

        final Button button = (Button) findViewById(R.id.btn_login);
        button.setOnClickListener(new View.OnClickListener() {           
            public void onClick(View v) {              

                user_name = username.getText().toString();
                pass_word = password.getText().toString();

                Intent goToNextActivity = new Intent(getApplicationContext(), menu.class);
                startActivity(goToNextActivity);


            }
        });                 


    }
}

那么我应该在两个文件中更新什么..

提前多多感谢

2 个答案:

答案 0 :(得分:3)

在数据库类中,您需要创建一个访问数据库的方法,例如

public boolean validateUser(String username, String password){
   Cursor c = getReadableDatabase().rawQuery(
            "SELECT * FROM " + TABLE_NAME + " WHERE "
                    + USER_NAME + "='" + username +"'AND "+PASSWORD+"='"+password+"'" ,  null);
   if (c.getCount>0)
      return true;
      return false;
}

您当然需要使用

之类的东西创建数据库表
public void createTable(SQLiteDatabase database) {
    String creationString = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME
            + " ( _id INTEGER PRIMARY KEY AUTOINCREMENT, "
            + USER_NAME + " TEXT NOT NULL, "
            + PASSWORD + " TEXT NOT NULL, "
           ");";
    database.execSQL(creationString);
}

并且包含TABLE_NAME之类的变量,这显然是您要为表格提供的名称,USER_NAME表示用户名列的名称,而PASSWORD也是如此。

您需要在数据库的onCreate中调用该createTable()方法。

要使用登录按钮使用数据库,您可以执行以下操作:

if (myDatabase.vaidateUser(user_name,pass_word)) {
    Intent goToNextActivity = new Intent(getApplicationContext(), menu.class);
    startActivity(goToNextActivity);

}
else
   Toast.makeText(mContext, "Wrong username/password", Toast.LENGTH_LONG).show();

希望这会有所帮助。我给你一个很好的起跑线。还有一些其他的东西你需要自己解决,但这应该让你很好地了解它是如何工作的,以及下一步去哪里(比如将记录添加到数据库等)。你需要创建一些变量等,但这并不困难!

答案 1 :(得分:1)

public void onClick(View v)方法应该调用一个方法来验证用户。

public void onClick(查看v){

            user_name = username.getText().toString();
            pass_word = password.getText().toString();
            if( validate(user_name, pasass_word));
             {               
              Intent goToNextActivity = new Intent(getApplicationContext(), menu.class);
             startActivity(goToNextActivity);
          }
        }




public boolean validate(String name,String password) {  
    String url = "http://n.n.n.n:nnnn/WebServicePro/services/Authentication";/*give Ip addres:portnumber*/
    String nameSpace="http://com.xxx.yy"; /**give proper  namespace*/
    String methoName= "authenticate";   /*This method is an operation in the webservice*/   
    SoapObject request = new  SoapObject(nameSpace,methoName);  

    request.addProperty("in0",name);
    request.addProperty("in1",password);

    try {
    resultsRequestSOAP = makeSoapCall(request, "", url);
       if(resultsRequestSOAP.toString().equalsIgnoreCase("true")){      
      return true;
            }
    else{     /*handle error*/}
    }catch (Exception exp) {            
        /*handle exception*/}
    return false;
}