项目简介:因代码太多只展现部分,如果有需要请联系qq:
382854425
,并备注XH-68
文件:DBUtil.JAVA
package com.example.studentsystem.util;
import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.example.studentsystem.MainActivity;
/**
* 链接数据库用的
*/
public class DBUtil extends SQLiteOpenHelper {
public static SQLiteDatabase db=null;
private static final String DATABASE_NAME="db_student.db";//数据库名字
private static final int DATABASE_VERSION=10;
public DBUtil(Context context){//用初始化创建数据库
super(context,DATABASE_NAME,null,DATABASE_VERSION,null);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL("PRAGMA foreign_keys = false");
db.execSQL("drop table if exists d_teacher");
//建立老师表
db.execSQL(" CREATE TABLE d_teacher (\n" +
" s_id varchar(20) primary key,\n" +
" s_pwd varchar(20),\n" +
" s_name varchar(20),\n" +
" s_sex varchar(20),\n" +
" s_bir varchar(20),\n" +
" s_pro varchar(20),\n" +
" s_zc varchar(20)\n" +
")");
db.execSQL(" INSERT INTO d_teacher VALUES ('202301','123456', '李四', '男', '1990年-10月-28日', '计算机', '教授');");
//-------------管理员 表
db.execSQL("drop table if exists d_admin");
db.execSQL(" CREATE TABLE d_admin (\n" +
" s_id varchar(20) primary key,\n" +
" s_pwd varchar(20) \n" +
")");
db.execSQL(" INSERT INTO d_admin VALUES ('root','root');");
db.execSQL("PRAGMA foreign_keys = true");
//---------------------班级表
db.execSQL("drop table if exists d_class");
db.execSQL(" CREATE TABLE d_class (\n" +
" s_id varchar(20) primary key,\n" +
" s_name varchar(20) ,\n" +
" s_id_teacher varchar(20),\n" +
" foreign key (s_id_teacher) references d_teacher(s_id) \n" +
")");
db.execSQL("INSERT INTO d_class VALUES ('1','1班','202301');");
//---------------------学生
db.execSQL("drop table if exists d_student");
db.execSQL(" CREATE TABLE d_student (\n" +
" s_id varchar(20) primary key,\n" +
" s_pwd varchar(20) ,\n" +
" s_name varchar(20) ,\n" +
" s_sex varchar(20) ,\n" +
" s_bir varchar(20) ,\n" +
" s_class_id varchar(20) ,\n" +
" s_in_school_date varchar(20),\n" +
" foreign key (s_class_id) references d_class(s_id) \n" +
")");
db.execSQL("INSERT INTO d_student VALUES ('1','123456','张三','男','2005年-6月-3日','1','2023年-6月-3日');");
db.execSQL("INSERT INTO d_student VALUES ('2','123456','李四','男','2005年-6月-3日','1','2023年-6月-3日');");
db.execSQL("INSERT INTO d_student VALUES ('3','123456','李四','女','2005年-6月-3日','1','2023年-6月-3日');");
//---------------------课程
db.execSQL("drop table if exists d_course");
db.execSQL(" CREATE TABLE d_course (\n" +
" s_id varchar(20),\n" +
" s_software_en varchar(20) ,\n" +
" s_c_lang varchar(20) ,\n" +
" s_java_lang varchar(20) ,\n" +
" s_man_sys varchar(20) ,\n" +
" s_android_dev varchar(20) ,\n" +
" foreign key (s_id) references d_student(s_id) \n" +
")");
db.execSQL("INSERT INTO d_course VALUES ('1','86','93','95','32','68');");
db.execSQL("PRAGMA foreign_keys = true");
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
onCreate(db);
}
}
文件:MainActivity.java
package com.example.studentsystem;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;
import com.example.studentsystem.dao.LoginDao;
import com.example.studentsystem.mainac.ManageActivity;
import com.example.studentsystem.mainac.StudentActivity;
import com.example.studentsystem.mainac.TeacherActivity;
import com.example.studentsystem.util.DBUtil;
import com.example.studentsystem.util.ToolsUtil;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//链接数据库
DBUtil dbUtil = new DBUtil(MainActivity.this);
SQLiteDatabase db = dbUtil.getWritableDatabase();//可以通过db进行操作数据库
DBUtil.db=db;
//编辑框 密码框 3个按钮内容获取道
EditText accountT=this.findViewById(R.id.login_username);
EditText pwdT=this.findViewById(R.id.login_password);
Button login = this.findViewById(R.id.login_button_login);//登录按钮
RadioButton man = findViewById(R.id.login_manage);
man.setChecked(true);
RadioButton tea = findViewById(R.id.login_teacher);
RadioButton stu = findViewById(R.id.login_student);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String account=ToolsUtil.editTextToString(accountT);
String pwd=ToolsUtil.editTextToString(pwdT);
//判断账号是否为空 密码是否为空
if (account.isEmpty()) {
Toast.makeText(MainActivity.this, "请输入账号/工号/学号", Toast.LENGTH_SHORT).show();
} else if (pwd.isEmpty()) {
Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
} else {
//根据单选按钮判断登录不同权限的账号
if (man.isChecked()) {
int res=LoginDao.LoginMan(db, account, pwd);
if(res==0){
Toast.makeText(MainActivity.this, "账号密码错误,请检查!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "登录管理员账号成功。", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(MainActivity.this, ManageActivity.class);//目标i界面
intent.putExtra("account",account);
startActivity(intent);//打开目标界面
}
} else if (tea.isChecked()) {
int res=LoginDao.LoginTea(db, account, pwd);
if(res==0){
Toast.makeText(MainActivity.this, "账号密码错误,请检查!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "登录老师账号成功。", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(MainActivity.this, TeacherActivity.class);//目标i界面
intent.putExtra("account",account);
startActivity(intent);//打开目标界面
}
} else if (stu.isChecked()) {
int res=LoginDao.LoginStu(db, account, pwd);
if(res==0){
Toast.makeText(MainActivity.this, "账号密码错误,请检查!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "登录学生账号成功", Toast.LENGTH_SHORT).show();
Intent intent =new Intent(MainActivity.this, StudentActivity.class);//目标i界面
intent.putExtra("account",account);
startActivity(intent);//打开目标界面
}
} else {
Toast.makeText(MainActivity.this, "请选择登录角色", Toast.LENGTH_SHORT).show();
}
}
}
});
}
}
文件:activity_main
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/login_backgroup_img"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="@+id/login_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="32dp"
android:text="学生管理系统"
android:textColor="#4CAF50"
android:textSize="40sp"
android:textStyle="bold"
/>
<EditText
android:id="@+id/login_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="@drawable/login_edit_background"
android:elevation="4dp"
android:hint="请输入学号/账号/工号"
android:inputType="text"
android:padding="12dp"
android:textColor="#4CAF50" />
<EditText
android:id="@+id/login_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="@drawable/login_edit_background"
android:elevation="4dp"
android:hint="请输入密码"
android:inputType="textPassword"
android:padding="12dp"
android:textColor="#4CAF50" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"
android:orientation="horizontal">
<RadioButton
android:id="@+id/login_manage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="管理员"
android:textColor="#4CAF50"
android:textSize="16sp"
android:textStyle="bold"
/>
<RadioButton
android:id="@+id/login_teacher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="老师"
android:textColor="#4CAF50"
android:textSize="16sp"
android:textStyle="bold" />
<RadioButton
android:id="@+id/login_student"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="学生"
android:textColor="#4CAF50"
android:textSize="16sp"
android:textStyle="bold" />
RadioGroup>
<Button
android:id="@+id/login_button_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="安全登录"
android:textSize="18sp"
android:background="@drawable/login_button_background"
android:textColor="#ffffff"
android:elevation="4dp"
android:layout_marginTop="16dp"
/>
LinearLayout>
RelativeLayout>
上面展示的代码只是部分代码,如果需要获取完整的,请联系QQ:382854425
,备注:XH-68