数据库连接,检查数据,登录页面

时间:2010-07-08 08:18:04

标签: sql android database

我在最后一年做了重大项目,我对Android加上我很新;我不擅长编码。我的登录页面需要帮助。我创建了类似数据库连接java文件的东西:

    package one.two;

import java.util.List;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;

    public class UserDB 
    {
     public String KEY_USERNAME = "Username";
     public  String KEY_PASSWORD = "Password";
     public String KEY_LNAME = "LastName";
     public String KEY_FNAME = "FirstName";

     private static final String DATABASE_NAME = "Users";
     private static final String DATABASE_TABLE = "User";

     private static final int DATABASE_VERSION = 1;

     private static Context context;

     private static DatabaseHelper DBHelper;
     private static SQLiteDatabase db;

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

     private static class DatabaseHelper extends SQLiteOpenHelper
     {
      DatabaseHelper(Context context)
      {
       super(context, "Users", null, 1);
      }

      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
      {
      }

      @Override
      public void onCreate(SQLiteDatabase db)
      {
      }
     }//end DatabaseHelper

     // ---opens the database---
     public UserDB open() throws SQLException
     {
      db = DBHelper.getWritableDatabase();
      return this;
     }

     // ---closes the database---
     public void close()
     {
      DBHelper.close();
     }


    }

我已经为使用SQLite的用户创建了一个数据库。数据库名称为Users,该表名为User。表中的记录是Username,Password,LastName,FirstName。我已将一个用户的信息插入数据库。问题是我不知道我的UserDB.java是否正确。

我还创建了login.java。硬编码登录页面:

    package one.two;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.TextView;



public class Login extends Activity implements OnClickListener{


 UserDB db = new UserDB(this);
/** Called when the activity is first created. */

 private EditText etUsername;
 private EditText etPassword;
 private Button btnLogin;
 //private Button btnRegister;
 private TextView lblResult;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.login);
     // Get the EditText and Button References
        etUsername = (EditText)findViewById(R.id.usernametxt);
        etPassword = (EditText)findViewById(R.id.passwordtxt);
        btnLogin = (Button)findViewById(R.id.btnLogin);
        //btnRegister = (Button)findViewById(R.id.btnRegister);
        lblResult = (TextView)findViewById(R.id.msglbl);

        //Cursor c = (Cursor) db.getAllTitles();

        Button btnArrival = (Button) findViewById(R.id.btnRegister);
     btnArrival.setOnClickListener(this);


    // Set Click Listener
    btnLogin.setOnClickListener(new OnClickListener() {
 @Override
 public void onClick(View v) {
  // Check Login
  String username = etUsername.getText().toString();
  String password = etPassword.getText().toString();

  if(username.equals("guest") && password.equals("guest")){
   lblResult.setText("Login successful.");
  } else {
   lblResult.setText("Login failed. Username and/or password doesn't match.");
  }
 }
});



 }

    public void onClick(View v)
 {
   Intent intent = new Intent(this, DatabaseActivity.class);
   startActivity(intent);
}

}

所以我想知道如何在login.java上应用数据库连接。我应该插入数据库连接,如db.Open();?几个月前我研究了ASP.Net,我忘了我学到的大部分内容。

那么我应该如何在login.java上打开数据库连接以及如何用数据库检查用户是否输入了正确的用户名和密码?

1 个答案:

答案 0 :(得分:1)