使用room进行数据库操作

master
liuyx 2 years ago
parent 50c9d580f8
commit 280bb1e94e

@ -6,7 +6,7 @@
<value-preview-pinned value="false" />
</component>
<component name="DBNavigator.Project.DatabaseBrowserManager">
<autoscroll-to-editor value="false" />
<autoscroll-to-editor value="true" />
<autoscroll-from-editor value="true" />
<show-object-properties value="true" />
<loaded-nodes />
@ -17,6 +17,21 @@
<component name="DBNavigator.Project.DatabaseFileManager">
<open-files />
</component>
<component name="DBNavigator.Project.ExecutionManager">
<retain-sticky-names value="false" />
</component>
<component name="DBNavigator.Project.MethodExecutionManager">
<method-browser />
<execution-history>
<group-entries value="true" />
<execution-inputs />
</execution-history>
<argument-values-cache />
</component>
<component name="DBNavigator.Project.ObjectQuickFilterManager">
<last-used-operator value="EQUAL" />
<filters />
</component>
<component name="DBNavigator.Project.Settings">
<connections />
<browser-settings>

@ -9,16 +9,27 @@ import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import cc.liuyx.app.dao.UserDao;
import cc.liuyx.app.db.UserDB;
import cc.liuyx.app.entity.User;
public class LoginActivity extends AppCompatActivity implements View.OnClickListener {
private EditText usernameEdit, passwdEdit;
private static UserDB db;
private UserDao userDao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
db = Room.databaseBuilder(getApplicationContext(), UserDB.class, "user")
.allowMainThreadQueries()
.build();
usernameEdit = findViewById(R.id.login_username);
passwdEdit = findViewById(R.id.login_passwd);
@ -39,22 +50,36 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
}
}
@Override
protected void onStop() {
super.onStop();
db.close();
}
private void login() {
String username = usernameEdit.getText().toString();
String passwd = passwdEdit.getText().toString();
userDao = db.getUserDao();
if (!username.equals("") && !passwd.equals("")) {
SharedPreferences sp = getSharedPreferences("loginStatus", MODE_PRIVATE);
String localName = sp.getString("username", null);
String localPasswd = sp.getString("passwd", null);
if (!username.equals(localName) || !passwd.equals(localPasswd)) {
Toast.makeText(LoginActivity.this, "用户名或密码错误>_<", Toast.LENGTH_SHORT).show();
// 在数据库中查询有无,如果有,则保存其登陆状态和信息
User user = userDao.findUserByName(username);
if (user == null) {
Toast.makeText(LoginActivity.this, "该用户还没有注册哦>_<", Toast.LENGTH_SHORT).show();
return;
}
if (!username.equals(user.getName()) || !passwd.equals(user.getPasswd())) {
Toast.makeText(LoginActivity.this, "您输入的密码有误", Toast.LENGTH_SHORT).show();
return;
}
SharedPreferences sp = getSharedPreferences("loginStatus", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putBoolean("isLoggedIn", true);
editor.putString("username", username);
editor.putString("username", user.getName());
editor.putString("email", user.getEmail());
editor.putString("phone", user.getPhone());
editor.apply();
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
@ -63,7 +88,5 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList
} else {
Toast.makeText(LoginActivity.this, "请填入用户名与密码哦~", Toast.LENGTH_SHORT).show();
}
}
}

@ -10,6 +10,11 @@ import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.room.Room;
import cc.liuyx.app.dao.UserDao;
import cc.liuyx.app.db.UserDB;
import cc.liuyx.app.entity.User;
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
@ -20,12 +25,19 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL
private String email = "";
private EditText usernameEdit, passwdEdit, passwdAgainEdit, phoneEdit, emailEdit;
private UserDao userDao;
private UserDB db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
db = Room.databaseBuilder(getApplicationContext(), UserDB.class, "user")
.allowMainThreadQueries()
.build();
userDao = db.getUserDao();
// 初始化每个组件
usernameEdit = findViewById(R.id.username);
passwdEdit = findViewById(R.id.passwd);
@ -45,6 +57,12 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL
});
}
@Override
protected void onStop() {
super.onStop();
db.close();
}
@Override
public void onClick(View v) {
if (v.getId() == R.id.btn_register) {
@ -64,6 +82,14 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL
// 判断两次密码是否相等
if (passwd.equals(passwdAgain)) {
if (!username.equals("") && !passwd.equals("")) {
// 向数据库中插入新用户的数据
// 如果用户名重复,则此次操作不被允许
if (userDao.findUserByName(username) != null) {
Toast.makeText(RegisterActivity.this, "已经存在该用户名啦", Toast.LENGTH_SHORT).show();
return;
}
userDao.insert(new User(username, passwd, phone, email));
// 设置登录状态
SharedPreferences sharedPreferences = getSharedPreferences("loginStatus", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
@ -71,7 +97,6 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL
editor.putString("username", username);
editor.putString("email", email);
editor.putString("phone", phone);
editor.putString("passwd", passwd);
editor.apply();
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);

@ -1,10 +1,30 @@
package cc.liuyx.app.dao;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
import cc.liuyx.app.entity.User;
@Dao
public interface UserDao {
@Insert
void insert();
void insert(User... users);
@Delete
void delete(User user);
@Update
void update(User user);
@Query("select * from user")
List<User> selectAllUser();
@Query("select * from user where name = :name")
User findUserByName(String name);
}

@ -0,0 +1,12 @@
package cc.liuyx.app.db;
import androidx.room.Database;
import androidx.room.RoomDatabase;
import cc.liuyx.app.dao.UserDao;
import cc.liuyx.app.entity.User;
@Database(entities = User.class, version = 1, exportSchema = false)
public abstract class UserDB extends RoomDatabase {
public abstract UserDao getUserDao();
}

@ -18,6 +18,29 @@ public class User {
@ColumnInfo(name = "email")
private String email;
public User() {
}
public User(String name, String passwd, String phone, String email) {
this.name = name;
this.passwd = passwd;
this.phone = phone;
this.email = email;
}
public User(String name, String passwd) {
this.name = name;
this.passwd = passwd;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}

Loading…
Cancel
Save