当前位置:   article > 正文

Android使用sqlite数据库实现登录与注册_android用数据库的登录注册,只有账号和密码

android用数据库的登录注册,只有账号和密码

layout:
在这里插入图片描述
在这里插入图片描述

javaBean

//学生类
public class Student {
    private  int   stu_id;   //学号
    private  String stu_name;   //姓名
    private char  stu_sex;//性别
    private  float weight;//体重
    private String stu_pwd;  //密码

    public Student(int stu_id, String stu_pwd) {
        this.stu_id = stu_id;
        this.stu_pwd = stu_pwd;
    }

    public Student(int stu_id, String stu_name, String stu_pwd) {
        this.stu_id = stu_id;
        this.stu_name = stu_name;
        this.stu_pwd = stu_pwd;
    }

    public String getStu_pwd() {
        return stu_pwd;
    }

    public void setStu_pwd(String stu_pwd) {
        this.stu_pwd = stu_pwd;
    }

    public float getWeight() {
        return weight;
    }

    public void setWeight(float weight) {
        this.weight = weight;
    }

    public int getStu_id() {
        return stu_id;
    }

    public void setStu_id(int stu_id) {
        this.stu_id = stu_id;
    }

    public String getStu_name() {
        return stu_name;
    }

    public void setStu_name(String stu_name) {
        this.stu_name = stu_name;
    }

    public char getStu_sex() {
        return stu_sex;
    }

    public void setStu_sex(char stu_sex) {
        this.stu_sex = stu_sex;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59

UsrDBHelper类

public class UsrDBHelper extends SQLiteOpenHelper {
    public static final String DB_NAME = "student.db";
    public static  final int DB_VERSION=1;
    public static final String TABLE_NAME="info";   //表名

    public UsrDBHelper( Context context) {
        super(context, DB_NAME, null, DB_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String create_table_sql="CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ("+
                "ID INTEGER PRIMARY KEY NOT NULL," +
                "NAME VARCHAR,SEX VARCHAR,WEIGHT REAL,PASSWORD VARCHAR)";

        db.execSQL(create_table_sql);

    }

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

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

Dao类

public class Dao {
    private UsrDBHelper mHelper;

    public Dao(Context context) {
        mHelper = new UsrDBHelper(context);
    }

    //注册  只需姓名学号和密码
    public void insert(Student student) {
        SQLiteDatabase db = mHelper.getWritableDatabase();

        String insert_sql = "INSERT INTO " + UsrDBHelper.TABLE_NAME +
                " (ID,NAME,PASSWORD) VALUES (?,?,?)";
        Object[] obj = {student.getStu_id(), student.getStu_name(), student.getStu_pwd()};

        db.execSQL(insert_sql, obj);
        db.close();
    }

    public String query(Student student) {   //查询某一行
        SQLiteDatabase db = mHelper.getReadableDatabase();

        String query_sql = "select * from " + UsrDBHelper.TABLE_NAME + " where id = " + student.getStu_id();
        Cursor cursor=db.rawQuery(query_sql,null);

        if (0 == cursor.getCount()) {
            return null;
        }
        cursor.moveToFirst();
        int stu_id= cursor.getInt(0);
        System.out.println(stu_id);
        String stu_pwd=  cursor.getString(4);

        cursor.close();
        db.close();
        return stu_id+";"+stu_pwd;

    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38

Activity

Login类

public class Login extends AppCompatActivity implements View.OnClickListener {

    private EditText et_stuId;
    private EditText et_stuPwd;
    private Button btn_login;
    private TextView tv_register;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stu_login);
        initView();
    }

    private void initView() {
        et_stuId = (EditText) findViewById(R.id.et_stuid);
        et_stuPwd = (EditText) findViewById(R.id.et_stupwd);
        btn_login = (Button) findViewById(R.id.btn_login);
        tv_register = (TextView) findViewById(R.id.tv_register);

        btn_login.setOnClickListener(this);
        tv_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:

                login();
                break;
            case R.id.tv_register:
                Intent intent=new Intent(this,Register.class);
                startActivity(intent);
        }
    }

    public void login() {
        String stu_id = et_stuId.getText().toString().trim();
        String stu_pwd = et_stuPwd.getText().toString().trim();
        if (TextUtils.isEmpty(stu_id) || TextUtils.isEmpty(stu_pwd)) {
            Toast.makeText(this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
        } else {

            Student student = new Student(Integer.parseInt(stu_id), stu_pwd);
            Dao dao = new Dao(this);
            String result = dao.query(student);
            if (result == null || result.length() == 0) {
                Toast.makeText(this, "用户不存在,请先注册", Toast.LENGTH_SHORT).show();
            } else{
                String[] a = result.split(";");
                String  id = a[0];
                String pwd = a[1];
                if (stu_id.equals(id)  && stu_pwd.equals(pwd)) {
                    Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(this, "用户或密码错误,登陆失败", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

Register类

public class Register extends AppCompatActivity implements View.OnClickListener {

    private EditText et_register_stuid;
    private EditText et_register_pwd;
    private EditText et_register_name;
    private Button btn_register;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.stu_register);
        initView();
    }

    private void initView() {
        et_register_stuid = (EditText) findViewById(R.id.et_register_stuid);
        et_register_pwd = (EditText) findViewById(R.id.et_register_pwd);
        et_register_name = (EditText) findViewById(R.id.et_register_name);
        btn_register = (Button) findViewById(R.id.btn_register);
        btn_register.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_register:
                register();
                break;
        }
    }

    public void register() {
        String  id=et_register_stuid.getText().toString().trim();
        String name=et_register_name.getText().toString().trim();
        String pwd=et_register_pwd.getText().toString().trim();

      if(TextUtils.isEmpty(id)||TextUtils.isEmpty(name)||TextUtils.isEmpty(pwd)){
            Toast.makeText(this, "不能为空", Toast.LENGTH_SHORT).show();
        }else{
            Student student= new Student(Integer.parseInt(id),name,pwd);
            Dao dao=new Dao(this);
            if(dao.query(student)!=null){
                Toast.makeText(this, "用户已存在,请直接登陆", Toast.LENGTH_SHORT).show();
            }else{
                dao.insert(student);
                Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
            }
           finish();
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/805133
推荐阅读
相关标签
  

闽ICP备14008679号