@TOC
I. Demand analysis
The student information management system has the function of adding student information, deleting student information, modifying student information and querying student information. Sqlite database is used to add, delete, change and check students’ information, and the login password is encrypted by MD5, which pays more attention to user privacy security. In addition to the welcome interface, there is also a registration and login interface, which meets the basic requirements of the application. The main interface and four independent operation interface jump smoothly. Android basics such as ListView, Sqlite, Handler, Intent, and SharedPreferences are used.
Second, development environment
Third, detailed design
3.1 Project Structure
Let’s take a look at the project structure. The package is called edu.cn studentnumbing, and the code files are stored in four folders:
- Activity: Responsible for adding, deleting, modifying, and checking the four activity files
- App: contains four activity files: welcome interface, login interface, registration interface and main interface
- Bean: Contains the entity class Student and the database action class StudentsDao
- Database :MD5 encryption utility class MD5Utils and database helper class MyDBHelper
3.2 database
SQLiteDatabase is one of the three main data storage methods for Android. The following is a template for SQLiteOpenHelper. As long as you modify the database name and the attributes of the database table, the database and data table will be created automatically when running the project. For example, our database is called db_student.db and our table is called tb_Students, Table attributes are studentid char(20) primary key, studentName varchar(20),majoy varchar(20), studentClass varchar(20).
package edu.cn.studentadminister.database;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MyDBHelper extends SQLiteOpenHelper {
// Define the database name and version number
public static final String name = "db_student.db";
public static final int DB_VERSION = 1;
// Create table statement
public static final String CREATE_USERDATA = "create table tb_Students(studentid char(20)primary key,studentname varchar(20),majoy varchar(20),studentclass varchar(20))";
// constructor
public MyDBHelper(Context context) {
super(context, name, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_USERDATA);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}}Copy the code
StudentsDao is used to add, delete, alter, and query tables:
- AddStudents () inserts key-value pairs of student information into the VALUES array, and then INSERT () inserts into the table.
- DeletStudents () deletes the student information with the specified ID from the data table.
- UpdateStudents () inserts key-value pairs of student information into the VALUES array, and update() updates the student information with the specified ID.
- GetStudents () gets all information about the student by id and uses cursor to scan a record from beginning to end.
- GetAllstudents () queries all student information records in the data table and returns a list of information.
// Add student information
public long addStudents(Student o) {
// Create a ContentValues object
ContentValues values = new ContentValues();
// Insert a key-value pair into the object
values.put("studentid", o.studentid);
values.put("studentname", o.studentname);
values.put("majoy", o.majoy);
values.put("studentclass", o.studentclass);
// Call the insert() method to insert data into the database
return db.insert("tb_Students".null, values);
}
// Delete the specified student information
public int deletStudents(Student o) {
return db.delete("tb_Students"."studentid=?".new String[]{String.valueOf(o.studentid)});
}
// Modify the specified student information
public int updateStudents(Student o) {
ContentValues value = new ContentValues();
value.put("studentname", o.studentname);
value.put("majoy", o.majoy);
value.put("studentclass", o.studentclass);
return db.update("tb_Students", value, "studentid=?".new String[]{String.valueOf(o.studentid)});
}
// Find information based on student id
public Student getStudents(String studentid) {
// Query the student
Cursor cursor = db.query("tb_Students".null."studentid=?".new String[]{studentid}, null.null.null);
Student o = new Student();
while (cursor.moveToNext()) {
o.studentid = cursor.getString(cursor.getColumnIndex("studentid"));
o.studentname = cursor.getString(cursor.getColumnIndex("studentname"));
o.majoy = cursor.getString(cursor.getColumnIndex("majoy"));
o.studentclass = cursor.getString(cursor.getColumnIndex("studentclass"));
}
return o;
}
// View all student information
public ArrayList<Map<String, Object>> getAllstudents() {
ArrayList<Map<String, Object>> listStudents = new ArrayList<Map<String, Object>>();
Cursor cursor = db.query("tb_Students".null.null.null.null.null.null);
int resultCounts = cursor.getCount(); / / record number
if (resultCounts == 0 ) {
return null;
} else {
while (cursor.moveToNext()) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("studentid", cursor.getString(cursor.getColumnIndex("studentid")));
map.put("studentname", cursor.getString(cursor.getColumnIndex("studentname")));
map.put("majoy", cursor.getString(cursor.getColumnIndex("majoy")));
map.put("studentclass", cursor.getString(cursor.getColumnIndex("studentclass")));
listStudents.add(map);
}
returnlistStudents; }}Copy the code
3.3 Login and Registration
Before login and registration, there is a 3S welcome interface for entering the application, which is very simple to implement. In fact, it uses the Handler message transfer mechanism to delay the execution of the intent jump in run() after 3000ms, and destroy its own activity.
public class WelcomeActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_welcome);
Handler handler = new Handler();
// When the timer ends, jump to the main screen
handler.postDelayed(new Runnable() {
@Override
public void run(a) {
Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
startActivity(intent);
WelcomeActivity.this.finish(); }},3000); }}Copy the code
The activity of login and registration interface has many details. First, the most basic requirement is that your login account and password must be correct, which requires searching the password of the login account from the database, and then comparing it with the password entered by the user. As mentioned earlier, the password is encrypted by MD5 and stored in SharedPreferences, so we also need to convert one step.
Secondly, the registered user account cannot already exist, that is, the user account is a key attribute, non-empty and unique. The entered account and password cannot be empty or contain invalid characters.
Finally, you cannot log in if the account does not exist. A message will be displayed whether the login is successful or not.
private void init(a) {
et_username = (EditText) findViewById(R.id.loginusername);
et_pwd = (EditText) findViewById(R.id.loginpwd);
save_pwd = (CheckBox) findViewById(R.id.save_pwd);
login = (Button)findViewById(R.id.loginBtn);
tv_register = (TextView) findViewById(R.id.register);
// Get the remembered account password
getUserInfo();
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Start logging in
getEditString();
Md5utils.md5 (); // Use MD5 to encrypt the password entered by the current user. PSW determines whether the encryption is consistent
String md5Psw= MD5Utils.md5(passWord);
// md5Psw ; SpPsw reads the password based on the user name from SharedPreferences
// Define the method readPsw to read the username and get the password
spPsw = readPsw(userName);
// TextUtils.isEmpty
if(TextUtils.isEmpty(userName)){
Toast.makeText( LoginActivity.this."Please enter user name", Toast.LENGTH_SHORT).show();
return;
}else if(TextUtils.isEmpty(passWord)){
Toast.makeText( LoginActivity.this."Please enter your password", Toast.LENGTH_SHORT).show();
return;
// md5Psw.equals(); Check whether the encrypted password is the same as that saved in the SharedPreferences
}else if(md5Psw.equals(spPsw)){
// Successful login
Toast.makeText( LoginActivity.this."welcome!"+ userName, Toast.LENGTH_SHORT).show();
SaveLoginStatus Boolean specifies the status, userName specifies the userName
saveLoginInfo(userName,passWord);
//getUserInfo();
saveLoginStatus(true, userName);
// Close the page and go to the home page
Intent data = new Intent();
//data.putExtra( ); name , value ;
data.putExtra("isLogin".true);
//RESULT_OK is the Activity system constant with the status code -1
SetResult returns data to the previous page. SetResult returns data to the previous page
setResult(RESULT_OK,data);
// Destroy the login screen
LoginActivity.this.finish();
// Jump to the main screen, and the status of successful login is passed to the MainActivity
startActivity(new Intent( LoginActivity.this, MainActivity.class));
return;
}else if((spPsw! =null&&! TextUtils.isEmpty(spPsw)&&! md5Psw.equals(spPsw))){ Toast.makeText( LoginActivity.this."Password error", Toast.LENGTH_SHORT).show();
return;
}else{
Toast.makeText( LoginActivity.this."This username does not exist", Toast.LENGTH_SHORT).show(); }}}); tv_register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// To jump to the registration interface and implement the registration function
Intent intent=new Intent( LoginActivity.this, RegisterActivity.class);
startActivity(intent);
LoginActivity.this.finish(); }}); }Copy the code
3.4 Add, delete, modify and check
Among the four activities, add, delete, change and check, we take AddActivity to add student information as an example. In fact, all activities operate on the student table in the database. First fill in the student information, click the “Add” button, then the following click event will be executed.
- Gets the text information for each EditText and converts it to a String.
- Verify that the information is correct and isEmpty() determines whether it isEmpty. If ID is empty, enter your student ID, if name is empty, enter your name, if Major is empty, and if class is empty, enter class.
- Initialize a Student object o and assign the String information to the properties of the object.
- The DAO object opens the database, performs the add information for the database, and then closes the database to end the activity.
@Override
public void onClick(View v) {
// When you click the "Add" button, you can get the added information
String studentid=etStudentid.getText().toString().trim();
String studentname = etStudentname.getText().toString().trim();
String majoy = etMajoy.getText().toString().trim();
String studentclass = etStudentclass.getText().toString();
// Verify that the information is correct
if (TextUtils.isEmpty(studentid)) {
Toast.makeText(this."Please enter your student number", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(studentname)) {
Toast.makeText(this."Please enter your name", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(majoy)) {
Toast.makeText(this."Please enter major", Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(studentclass)) {
Toast.makeText(this."Please enter class", Toast.LENGTH_SHORT).show();
return;
}
// Add student information
Student o =new Student();
o.studentid= studentid;
o.studentname = studentname;
o.majoy = majoy;
o.studentclass = studentclass;
// Create a database access object
StudentsDAO dao = new StudentsDAO(getApplicationContext());
// Open the database
dao.open();
// Execute the database access method
long result = dao.addStudents(o);
if (result > 0) {
Toast.makeText(this."Added successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this."Add failed", Toast.LENGTH_SHORT).show();
}
// Close the database
dao.close();
// Close the activity
finish();
}
Copy the code
Iv. Project demonstration
[video (video – uMKP61Jc – 1642045783143) (type – CSDN) (url-live.csdn.net/v/embed/184…)”
Five, the project source code
Click here to download the source code: 👉Android Studio to achieve student information management system 👈
☀️ can also follow my public account “Mengxin Gas Station”, background reply: student system ☀️
🚀 Here’s something you missed
-
❤️Android Studio implements a secondary transaction system on campus ❤️
-
❤️Android Stduio to implement a weather forecast APP❤️
-
❤️Android Studio implements a healthy eating tie-in APP❤️
-
❤️Android Studio implements music player version 2.0 ❤️