虽然登录注册界面做好
但是用户注册后需要将信息记录到数据库之后
每次登录需要通过数据库读取数据才可以登录成功,否则登录失败
DatabaseHelper.Java
package com.example.fuxing.service; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { static String name="user.db"; static int dbVersion=1; public DatabaseHelper(Context context) { super(context, name, null, dbVersion); } public void onCreate(SQLiteDatabase db) { String sql="create table user(id integer primary key autoincrement,username varchar(20),password varchar(20),email varchar(50),phonenumber varchar(11))"; db.execSQL(sql); }//创建个人信息表 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
UserService.java
package com.example.fuxing.service; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.fuxing.User; public class UserService { private DatabaseHelper dbHelper; public UserService(Context context){ dbHelper=new DatabaseHelper(context); } public boolean login(String username,String password){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="select * from user where username=? and password=?"; Cursor cursor=sdb.rawQuery(sql, new String[]{username,password}); if(cursor.moveToFirst()==true){ cursor.close(); return true; } return false; } public boolean register(User user){ SQLiteDatabase sdb=dbHelper.getReadableDatabase(); String sql="insert into user(username,password,email,phonenumber) values(?,?,?,?)"; Object obj[]={user.getUsername(),user.getPassword(),user.getEmail(),user.getPhonenumber()}; sdb.execSQL(sql, obj); return true; } }
User.java
package com.example.fuxing; import java.io.Serializable; public class User implements Serializable{ private int id; private String username; private String password; private String email; private String phonenumber; public User() { super(); // TODO Auto-generated constructor stub } public User(String username, String password, String email, String phonenumber) { super(); this.username = username; this.password = password; this.email = email; this.phonenumber = phonenumber; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPhonenumber() { return phonenumber; } public void setPhonenumber(String phonenumber) { this.phonenumber = phonenumber; } @Override public String toString() { return "User [id=" + id + ", username=" + username + ", password=" + password + ", email=" + email + ", phonenumber=" + phonenumber + "]"; } }