1、项目要求
1.1数据库表描述
设计一个教务管理系统,要求如下:
系统涉及的表有
account表(账号表)
teacher表(教师表)
student表(学生表)
course表 (课程表)
score表(成绩表)
clazz表(班级表)
表中的字段,根据系统功能,自行设计,注意字段的约束,和主外键关系
1.2账号描述
该系统账号角色有3个,分别为管理员、老师、学生
管理员可以添加、修改、删除老师和学生的全部信息,也可以查看所有老师和学生的全部信息
老师只能查看、修改、删除自己的全部信息,可以查看自己班级学生的全部信息、修改、删除、添加某个学生的信息
学生只能查看、修改、删除自己的全部信息
1.3功能描述
功能描述如下:
系统启动后,可以选择角色进行登录,只有账号和密码正确才能继续使用系统,否则一直在登录页面
管理员登录成功后,可以根据功能选项对老师、学生信息进行增删改查操作
查看所有班级信息
查看指定班级信息
修改指定班级信息
添加班级信息
删除指定班级信息
查看所有课程信息
查看指定课程信息
修改指定课程信息
添加课程信息
删除指定课程信息
查看所有老师信息
查看指定老师信息
修改指定老师信息
删除指定老师信息
添加老师信息
查看所有学生信息
查看指定学生信息
添加学生信息
修改指定学生信息
删除指定学生信息
查看所有学生成绩信息
查看指定学生成绩信息
查看指定班级所有学生成绩信息
查看所有班级指定科目成绩信息
查看指定班级指定科目成绩信息
老师登录成功后,可以根据功能选项对自己的信息进行增删改查操作,可以对所有学生信息进行增删改查操作
查看自己的信息
修改自己的信息
删除自己的信息
查看自己班级所有学生信息
查看自己班级指定学生信息
添加自己班级学生信息
修改自己班级指定学生信息
删除自己班级指定学生信息
查看自己班级所有学生所有科目成绩信息
查看自己班级所有学生指定科目成绩信息
查看自己班级指定学生所有科目成绩信息
查看自己班级指定学生指定科目成绩信息
学生登录成功后,可以根据功能选项对自己的信息进行查看、修改、删除操作
查看自己的信息
修改自己的信息
删除自己的信息
查看自己所有科目成绩信息
查看自己指定科目成绩信息
查看自己班级所有学生所有科目成绩信息
查看自己班级所有学生指定科目成绩信息
查看自己班级指定学生所有科目成绩信息
查看自己班级指定学生指定科目成绩信息
2、数据库表设计
2.1实现代码
CREATE DATABASE IF NOT EXISTS eamsystem; #Educational Administration Management System
USE eamsystem;
# account表(账号表)
CREATE TABLE IF NOT EXISTS account(
atid INT PRIMARY KEY,
atname VARCHAR(20) NOT NULL,
atpassword VARCHAR(20) NOT NULL
);
INSERT INTO account(atid,atname,atpassword)
VALUES
(101,'墨尘','ad001'),
(102,'修远','ad002'),
(103,'白洛','ad003'),
(104,'思羽','ad004');
#clazz表(班级表)
CREATE TABLE IF NOT EXISTS clazz(
clazzid INT PRIMARY KEY,
clname VARCHAR(20) NOT NULL
);
INSERT INTO clazz VALUES(11,'1班'),(12,'2班'),(13,'3班');
# teacher表
CREATE TABLE IF NOT EXISTS teacher(
teacherid INT PRIMARY KEY ,
thname VARCHAR(20) NOT NULL,
thage INT NOT NULL,
thsex CHAR NOT NULL,
clazzid INT , # 外键约束 同步修改,删除设置为null
thpassword VARCHAR(20) NOT NULL ,
CONSTRAINT teacher_clid FOREIGN KEY (clazzid) REFERENCES clazz(clazzid) ON UPDATE CASCADE ON DELETE RESTRICT
);
INSERT INTO teacher VALUES
(201,'老师1',30,'男',11,'th001'),
(202,'老师2',40,'男',11,'th002'),
(203,'老师3',28,'女',12,'th003'),
(204,'老师4',29,'女',12,'th004'),
(205,'老师5',30,'女',12,'th005'),
(206,'老师6',40,'女',13,'th006');
#student表
CREATE TABLE IF NOT EXISTS student(
studentid INT PRIMARY KEY ,
stname VARCHAR(20) NOT NULL ,
stsex CHAR NOT NULL ,
stage INT NOT NULL,
clazzid INT,# 外键约束 同步修改,删除设置为null
stpassword VARCHAR(20) NOT NULL ,
CONSTRAINT student_clid FOREIGN KEY (clazzid) REFERENCES clazz(clazzid) ON UPDATE CASCADE ON DELETE RESTRICT
);
INSERT INTO student VALUES
(301,'学生1','男',15,11,'st001'),
(302,'学生2','男',14,11,'st002'),
(303,'学生3','女',14,11,'st003'),
(304,'学生4','男',14,12,'st004'),
(305,'学生5','男',14,12,'st005'),
(306,'学生6','女',14,12,'st006'),
(307,'学生7','女',14,12,'st007'),
(308,'学生8','女',14,13,'st008'),
(309,'学生9','女',14,13,'st009'),
(310,'学生10','男',14,13,'st10');
#course表(课程表)
CREATE TABLE IF NOT EXISTS course(
courseid INT PRIMARY KEY,
csname VARCHAR(20) NOT NULL,
teacherid INT, #修改同步,删除删除设置为null
CONSTRAINT coutse_tid FOREIGN KEY (teacherid) REFERENCES teacher(teacherid) ON UPDATE CASCADE ON DELETE RESTRICT
);
#[CONSTRAINT <外键约束名称>] FOREIGN KEY(从表的某个字段) references 主表名(被参考字段)
#CONSTRAINT fk_emp1_dept_id FOREIGN KEY (department_id) REFERENCES dept1(dept_id)
INSERT INTO course VALUES
(401,'Python',201),
(402,'C#',202),
(403,'JAVA',203),
(404,'Go',204),
(405,'C语言',205),
(406,'Hadoop',206);
#score表(成绩表)
CREATE TABLE IF NOT EXISTS score(
studentid INT NOT NULL, # 修改同步,删除同步,删除学生信息的时候要删除对应成绩信息
courseid INT , # 修个课程编号时同步,删除设置为null
grade INT ,
CONSTRAINT score_stid FOREIGN KEY (studentid) REFERENCES student(studentid) ON UPDATE CASCADE ON DELETE CASCADE ,
CONSTRAINT score_csid FOREIGN KEY (courseid) REFERENCES course(courseid) ON UPDATE CASCADE ON DELETE RESTRICT
);
INSERT INTO score (studentid,courseid,grade) VALUES(301,401,60);
INSERT INTO score (studentid,courseid,grade) VALUES(301,402,70);
INSERT INTO score (studentid,courseid,grade) VALUES(301,403,88);
INSERT INTO score (studentid,courseid,grade) VALUES(301,404,99);
INSERT INTO score (studentid,courseid,grade) VALUES(301,405,50);
INSERT INTO score (studentid,courseid,grade) VALUES(301,406,80);
INSERT INTO score (studentid,courseid,grade) VALUES(302,401,77);
INSERT INTO score (studentid,courseid,grade) VALUES(302,402,99);
INSERT INTO score (studentid,courseid,grade) VALUES(302,403,88);
INSERT INTO score (studentid,courseid,grade) VALUES(302,404,66);
INSERT INTO score (studentid,courseid,grade) VALUES(302,405,60);
INSERT INTO score (studentid,courseid,grade) VALUES(302,406,70);
INSERT INTO score (studentid,courseid,grade) VALUES(303,401,90);
INSERT INTO score (studentid,courseid,grade) VALUES(303,402,80);
INSERT INTO score (studentid,courseid,grade) VALUES(303,403,88);
INSERT INTO score (studentid,courseid,grade) VALUES(303,404,77);
INSERT INTO score (studentid,courseid,grade) VALUES(303,405,66);
INSERT INTO score (studentid,courseid,grade) VALUES(303,406,50);
INSERT INTO score (studentid,courseid,grade) VALUES(304,401,80);
INSERT INTO score (studentid,courseid,grade) VALUES(304,402,40);
INSERT INTO score (studentid,courseid,grade) VALUES(304,403,80);
INSERT INTO score (studentid,courseid,grade) VALUES(304,404,97);
INSERT INTO score (studentid,courseid,grade) VALUES(304,405,50);
INSERT INTO score (studentid,courseid,grade) VALUES(304,406,88);
INSERT INTO score (studentid,courseid,grade) VALUES(305,401,77);
INSERT INTO score (studentid,courseid,grade) VALUES(305,402,88);
INSERT INTO score (studentid,courseid,grade) VALUES(305,403,90);
INSERT INTO score (studentid,courseid,grade) VALUES(305,404,68);
INSERT INTO score (studentid,courseid,grade) VALUES(305,405,76);
INSERT INTO score (studentid,courseid,grade) VALUES(305,406,89);
INSERT INTO score (studentid,courseid,grade) VALUES(306,401,75);
INSERT INTO score (studentid,courseid,grade) VALUES(306,402,77);
INSERT INTO score (studentid,courseid,grade) VALUES(306,403,88);
INSERT INTO score (studentid,courseid,grade) VALUES(306,404,90);
INSERT INTO score (studentid,courseid,grade) VALUES(306,405,64);
INSERT INTO score (studentid,courseid,grade) VALUES(306,406,50);
INSERT INTO score (studentid,courseid,grade) VALUES(307,401,70);
INSERT INTO score (studentid,courseid,grade) VALUES(307,402,90);
INSERT INTO score (studentid,courseid,grade) VALUES(307,403,90);
INSERT INTO score (studentid,courseid,grade) VALUES(307,404,88);
INSERT INTO score (studentid,courseid,grade) VALUES(307,405,99);
INSERT INTO score (studentid,courseid,grade) VALUES(307,406,95);
INSERT INTO score (studentid,courseid,grade) VALUES(308,401,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,402,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,403,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,404,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,405,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,406,50);
INSERT INTO score (studentid,courseid,grade) VALUES(309,401,96);
INSERT INTO score (studentid,courseid,grade) VALUES(309,402,75);
INSERT INTO score (studentid,courseid,grade) VALUES(309,403,51);
INSERT INTO score (studentid,courseid,grade) VALUES(309,404,76);
INSERT INTO score (studentid,courseid,grade) VALUES(309,405,85);
INSERT INTO score (studentid,courseid,grade) VALUES(309,406,64);
INSERT INTO score (studentid,courseid,grade) VALUES(310,401,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,402,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,403,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,404,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,405,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,406,100);
/*
管理员登录成功后,可以根据功能选项对老师、学生信息进行增删改查操作*/
#查看所有班级信息
SELECT * FROM clazz;
#查看指定班级信息
SELECT * FROM clazz WHERE clazzid = 11;
#修改指定班级信息
SELECT * FROM clazz;
UPDATE clazz SET clname = '1班' WHERE clazzid = 11;
UPDATE clazz SET clname = '5班' WHERE clazzid = 11;
#添加班级信息
INSERT INTO clazz(clazzid,clname) VALUE(14,'4班');
#删除指定班级信息
DELETE FROM clazz WHERE clazzid=14;
#查看所有课程信息
SELECT * FROM course;
#查看指定课程信息
SELECT * FROM course WHERE courseid=401;
#修改指定课程信息
SELECT * FROM course;
UPDATE course SET csname = 'Python',teacherid = 201 WHERE courseid = 401;
UPDATE course SET csname = '语文' , teacherid = 201 WHERE courseid = 401;
#添加课程信息
INSERT INTO course(courseid,csname,teacherid) VALUE (407,'数学',206);
#删除指定课程信息
DELETE FROM course WHERE courseid=407;
#查看所有老师信息
SELECT * FROM teacher ;
#查看指定老师信息
SELECT * FROM teacher WHERE teacherid =201 ;
#修改指定老师信息
UPDATE teacher SET thname='老师1',thage=30,thsex='男',clazzid=11,thpassword='th001' WHERE teacherid = 201;
UPDATE teacher SET thname='老师8',thage=30,thsex='女',clazzid=11,thpassword='th001' WHERE teacherid = 201;
#添加老师信息
INSERT INTO teacher(teacherid,thname,thage,thsex,clazzid,thpassword) VALUES(207,'老师7',40,'男',13,'8888');
#删除指定老师信息
DELETE FROM teacher WHERE teacherid=207;
#查看所有学生信息
SELECT * FROM student;
#查看指定学生信息
SELECT * FROM student WHERE studentid=301;
#修改指定学生信息
UPDATE student SET stname='学生1',stsex='男',stage=15,clazzid=11,stpassword='st001' WHERE studentid = 301;
UPDATE student SET stname='我是学生',stsex='男',stage=15,clazzid=11,stpassword='st001' WHERE studentid = 301;
#添加学生信息
INSERT INTO student(studentid,stname,stsex,stage,clazzid,stpassword) VALUES(311,'学生55','女',14,13,'st008');
#删除指定学生信息
DELETE FROM student WHERE studentid=311;
#查看所有学生成绩信息
SELECT * FROM score;
#查看指定学生成绩信息
SELECT * FROM score WHERE studentid=301;
#查看指定班级所有学生成绩信息
SELECT b.studentid,courseid,grade
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND a.`clazzid`=11;
#查看所有班级指定科目成绩信息
SELECT b.`studentid`,b.`courseid`,b.`grade`
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND b.`courseid`=401;
#查看指定班级指定科目成绩信息
SELECT a.clname '班级名称' , c.`studentid` '学生编号' ,b.`courseid` '课程编号',b.grade '成绩'
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND b.`courseid`=401 AND a.`clazzid`=11 ;
/*
老师登录成功后,可以根据功能选项对自己的信息进行增删改查操作,可以对所有学生信息进行增删改查操作*/
#查看自己的信息
SELECT * FROM teacher WHERE teacherid = 201;
#修改自己的信息
UPDATE teacher SET thname='老师1',thage=30,thsex='男',clazzid=11,thpassword='th001' WHERE teacherid = 201;
UPDATE teacher SET thname='老师8',thage=30,thsex='女',clazzid=11,thpassword='th001' WHERE teacherid = 201;
#删除自己的信息
/*
SET FOREIGN_KEY_CHECKS = 0; // 先设置外键约束检查关闭
DROP TABLE table1;
detele FROM TABLE WHERE ;
// 删除表或数据
SET FOREIGN_KEY_CHECKS = 1; // 开启外键约束检查,以保持表结构完整性
*/
SET FOREIGN_KEY_CHECKS = 0; // 先设置外键约束检查关闭
DELETE FROM teacher WHERE teacherid=201;
INSERT INTO teacher VALUES
(201,'老师1',30,'男',11,'th001');
SET FOREIGN_KEY_CHECKS = 1; // 开启外键约束检查,以保持表结构完整性
SELECT * FROM teacher;
#查看自己班级所有学生信息
SELECT *
FROM student
WHERE clazzid=11;
#查看自己班级指定学生信息
SELECT *
FROM student
WHERE clazzid = 11 AND studentid=301;
#添加自己班级学生信息
SELECT clazzid FROM teacher WHERE teacherid=201;
INSERT INTO student(studentid,stname,stsex,stage,clazzid,stpassword)
VALUES(311,'dddd','男',14,11,'st002');
SELECT * FROM student;
#修改自己班级指定学生信息
SELECT clazzid FROM teacher WHERE teacherid=201;
UPDATE student SET stname='学生1',stsex='男',stage=15,clazzid=11,stpassword='st001' WHERE studentid = 301;
UPDATE student SET stname='我是学生',stsex='男',stage=15,clazzid=11,stpassword='st001' WHERE studentid = 301;
#删除自己班级指定学生信息
SET FOREIGN_KEY_CHECKS = 0; // 先设置外键约束检查关闭
DELETE FROM student WHERE studentid=311;
SET FOREIGN_KEY_CHECKS = 1; // 开启外键约束检查,以保持表结构完整性
INSERT INTO student VALUES
(301,'学生1','男',15,11,'st001');
#下述功能重复,这里就没有写了
#查看自己班级所有学生所有科目成绩信息
#查看自己班级所有学生指定科目成绩信息
#查看自己班级指定学生所有科目成绩信息
#查看自己班级指定学生指定科目成绩信息
/*
学生登录成功后,可以根据功能选项对自己的信息进行查看、修改、删除操作
*/
#查看自己的信息
SELECT * FROM student WHERE studentid=301;
#修改自己的信息
UPDATE student SET stname='学生10',stsex='男',stage=15,clazzid=11,stpassword='st010' WHERE studentid = 301;
UPDATE student SET stname='我是学生',stsex='男',stage=15,clazzid=11,stpassword='st010' WHERE studentid = 301;
#删除自己的信息
DELETE FROM student WHERE studentid=302;
#查看自己所有科目成绩信息
SELECT * FROM score WHERE studentid=301;
#查看自己指定科目成绩信息
SELECT * FROM score WHERE studentid=301 AND courseid=401 ;
#查看自己班级所有学生所有科目成绩信息
SELECT b.studentid,b.`courseid`,b.`grade`
FROM student a ,score b,course c
WHERE a.`studentid` = b.`studentid` AND b.`courseid` = c.`courseid` AND a.`clazzid`=11;
#查看自己班级所有学生指定科目成绩信息
SELECT b.studentid,b.`courseid`,b.`grade`
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND b.`courseid`=401 AND a.`clazzid`=11 ;
#查看自己班级指定学生所有科目成绩信息
SELECT b.studentid,b.`courseid`,b.`grade`
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND c.`studentid`=301 AND a.`clazzid`=11 ;
#查看自己班级指定学生指定科目成绩信息
SELECT b.studentid,b.`courseid`,b.`grade`
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND a.`clazzid`=11 AND c.`studentid`=301 AND b.`courseid`=401 ;
2.2数据库设计
2.2.1account表(管理员账号表)
CREATE TABLE IF NOT EXISTS account(
atid INT PRIMARY KEY,
atname VARCHAR(20) NOT NULL,
atpassword VARCHAR(20) NOT NULL
);
字段名 | 含义 | 类型 | 主键 | 外键 | 非空 |
atid | 管理员账号 | INT | 是 | 否 | 是 |
atname | 管理员姓名 | VARCHAR(20) | 否 | 否 | 是 |
atpassword | 管理员密码 | VARCHAR(20) | 否 | 否 | 是 |
account表 初始数据
INSERT INTO account(atid,atname,atpassword)
VALUES
(101,'墨尘','ad001'),
(102,'修远','ad002'),
(103,'白洛','ad003'),
(104,'思羽','ad004');
2.2.2clazz表(班级表)
CREATE TABLE IF NOT EXISTS clazz(
clazzid INT PRIMARY KEY,
clname VARCHAR(20) NOT NULL
);
字段名 | 含义 | 类型 | 主键 | 外键 | 非空 |
clazzid | 班级编号 | INT | 是 | 否 | 是 |
clname | 班级名称 | VARCHAR(20) | 否 | 否 | 是 |
clazz表 初始数据
INSERT INTO clazz VALUES(11,'1班'),(12,'2班'),(13,'3班');
2.2.3teacher表(教师表)
CREATE TABLE IF NOT EXISTS teacher(
teacherid INT PRIMARY KEY ,
thname VARCHAR(20) NOT NULL,
thage INT NOT NULL,
thsex CHAR NOT NULL,
clazzid INT , # 外键约束 同步修改,删除设置为null
thpassword VARCHAR(20) NOT NULL ,
CONSTRAINT teacher_clid FOREIGN KEY (clazzid) REFERENCES clazz(clazzid) ON UPDATE CASCADE ON DELETE RESTRICT
);
字段名 | 含义 | 类型 | 主键 | 外键 | 非空 |
teacherid | 教师账号 | INT | 是 | 否 | 是 |
thname | 教师姓名 | VARCHAR(20) | 否 | 否 | 是 |
thage | 教师年龄 | INT | 否 | 否 | 是 |
thsex | 教师性别 | CHAR | 否 | 否 | 是 |
clazzid | 所带班级编号 | INT | 否 | 是 | 否 |
thpassword | 账号密码 | VARCHAR(20) | 否 | 否 | 是 |
teacher表 初始数据
INSERT INTO teacher VALUES
(201,'老师1',30,'男',11,'th001'),
(202,'老师2',40,'男',11,'th002'),
(203,'老师3',28,'女',12,'th003'),
(204,'老师4',29,'女',12,'th004'),
(205,'老师5',30,'女',12,'th005'),
(206,'老师6',40,'女',13,'th006');
2.2.4student表(学生表)
#student表
CREATE TABLE IF NOT EXISTS student(
studentid INT PRIMARY KEY ,
stname VARCHAR(20) NOT NULL ,
stsex CHAR NOT NULL ,
stage INT NOT NULL,
clazzid INT,# 外键约束 同步修改,删除设置为null
stpassword VARCHAR(20) NOT NULL ,
CONSTRAINT student_clid FOREIGN KEY (clazzid) REFERENCES clazz(clazzid) ON UPDATE CASCADE ON DELETE RESTRICT
);
字段名 | 含义 | 类型 | 主键 | 外键 | 非空 |
studentid | 学生账号 | INT | 是 | 否 | 是 |
stname | 学生姓名 | VARCHAR(20) | 否 | 否 | 是 |
stsex | 学生性别 | CHAR | 否 | 否 | 是 |
stage | 学生年龄 | INT | 否 | 否 | 是 |
clazzid | 所在班级编号 | INT | 否 | 是 | 否 |
stpassword | 密码 | VARCHAR(20) | 否 | 否 | 是 |
student表初始数据
INSERT INTO student VALUES
(301,'学生1','男',15,11,'st001'),
(302,'学生2','男',14,11,'st002'),
(303,'学生3','女',14,11,'st003'),
(304,'学生4','男',14,12,'st004'),
(305,'学生5','男',14,12,'st005'),
(306,'学生6','女',14,12,'st006'),
(307,'学生7','女',14,12,'st007'),
(308,'学生8','女',14,13,'st008'),
(309,'学生9','女',14,13,'st009'),
(310,'学生10','男',14,13,'st10');
2.2.5course表(课程表)
#course表(课程表)
CREATE TABLE IF NOT EXISTS course(
courseid INT PRIMARY KEY,
csname VARCHAR(20) NOT NULL,
teacherid INT, #修改同步,删除删除设置为null
CONSTRAINT coutse_tid FOREIGN KEY (teacherid) REFERENCES teacher(teacherid) ON UPDATE CASCADE ON DELETE RESTRICT
);
字段名 | 含义 | 类型 | 主键 | 外键 | 非空 |
courseid | 课程编号 | INT | 是 | 否 | 是 |
csname | 课程名称 | VARCHAR(20) | 否 | 否 | 是 |
teacherid | 教师账号(编号) | INT | 否 | 是 | 否 |
course表初始数据
INSERT INTO course VALUES
(401,'Python',201),
(402,'C#',202),
(403,'JAVA',203),
(404,'Go',204),
(405,'C语言',205),
(406,'Hadoop',206);
2.2.6score表(成绩表)
字段名 | 含义 | 类型 | 主键 | 外键 | 非空 |
studentid | INT | 否 | 是 | 是 | |
courseid | INT | 否 | 是 | 否 | |
grade | INT | 否 | 否 | 否 |
score表初始数据
INSERT INTO score (studentid,courseid,grade) VALUES(301,401,60);
INSERT INTO score (studentid,courseid,grade) VALUES(301,402,70);
INSERT INTO score (studentid,courseid,grade) VALUES(301,403,88);
INSERT INTO score (studentid,courseid,grade) VALUES(301,404,99);
INSERT INTO score (studentid,courseid,grade) VALUES(301,405,50);
INSERT INTO score (studentid,courseid,grade) VALUES(301,406,80);
INSERT INTO score (studentid,courseid,grade) VALUES(302,401,77);
INSERT INTO score (studentid,courseid,grade) VALUES(302,402,99);
INSERT INTO score (studentid,courseid,grade) VALUES(302,403,88);
INSERT INTO score (studentid,courseid,grade) VALUES(302,404,66);
INSERT INTO score (studentid,courseid,grade) VALUES(302,405,60);
INSERT INTO score (studentid,courseid,grade) VALUES(302,406,70);
INSERT INTO score (studentid,courseid,grade) VALUES(303,401,90);
INSERT INTO score (studentid,courseid,grade) VALUES(303,402,80);
INSERT INTO score (studentid,courseid,grade) VALUES(303,403,88);
INSERT INTO score (studentid,courseid,grade) VALUES(303,404,77);
INSERT INTO score (studentid,courseid,grade) VALUES(303,405,66);
INSERT INTO score (studentid,courseid,grade) VALUES(303,406,50);
INSERT INTO score (studentid,courseid,grade) VALUES(304,401,80);
INSERT INTO score (studentid,courseid,grade) VALUES(304,402,40);
INSERT INTO score (studentid,courseid,grade) VALUES(304,403,80);
INSERT INTO score (studentid,courseid,grade) VALUES(304,404,97);
INSERT INTO score (studentid,courseid,grade) VALUES(304,405,50);
INSERT INTO score (studentid,courseid,grade) VALUES(304,406,88);
INSERT INTO score (studentid,courseid,grade) VALUES(305,401,77);
INSERT INTO score (studentid,courseid,grade) VALUES(305,402,88);
INSERT INTO score (studentid,courseid,grade) VALUES(305,403,90);
INSERT INTO score (studentid,courseid,grade) VALUES(305,404,68);
INSERT INTO score (studentid,courseid,grade) VALUES(305,405,76);
INSERT INTO score (studentid,courseid,grade) VALUES(305,406,89);
INSERT INTO score (studentid,courseid,grade) VALUES(306,401,75);
INSERT INTO score (studentid,courseid,grade) VALUES(306,402,77);
INSERT INTO score (studentid,courseid,grade) VALUES(306,403,88);
INSERT INTO score (studentid,courseid,grade) VALUES(306,404,90);
INSERT INTO score (studentid,courseid,grade) VALUES(306,405,64);
INSERT INTO score (studentid,courseid,grade) VALUES(306,406,50);
INSERT INTO score (studentid,courseid,grade) VALUES(307,401,70);
INSERT INTO score (studentid,courseid,grade) VALUES(307,402,90);
INSERT INTO score (studentid,courseid,grade) VALUES(307,403,90);
INSERT INTO score (studentid,courseid,grade) VALUES(307,404,88);
INSERT INTO score (studentid,courseid,grade) VALUES(307,405,99);
INSERT INTO score (studentid,courseid,grade) VALUES(307,406,95);
INSERT INTO score (studentid,courseid,grade) VALUES(308,401,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,402,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,403,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,404,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,405,50);
INSERT INTO score (studentid,courseid,grade) VALUES(308,406,50);
INSERT INTO score (studentid,courseid,grade) VALUES(309,401,96);
INSERT INTO score (studentid,courseid,grade) VALUES(309,402,75);
INSERT INTO score (studentid,courseid,grade) VALUES(309,403,51);
INSERT INTO score (studentid,courseid,grade) VALUES(309,404,76);
INSERT INTO score (studentid,courseid,grade) VALUES(309,405,85);
INSERT INTO score (studentid,courseid,grade) VALUES(309,406,64);
INSERT INTO score (studentid,courseid,grade) VALUES(310,401,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,402,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,403,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,404,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,405,100);
INSERT INTO score (studentid,courseid,grade) VALUES(310,406,100);
2.3E-R图
3、JAVA后端+JDBC+三层结构实现
3.1依赖包
说明:可根据自己电脑安装的mysql版本自行搜索下载对应版本的包依赖,这里就不提供依赖包。
3.2Druid连接池的database.properties配置文件
# 连接设置 driver=com.mysql.cj.jdbc.Driver #eamsystem 是上文创建的数据库,六张表都在这个数据库中 url=jdbc:mysql://localhost:3306/eamsystem username=你自己的数据库用户名 password=你自己的密码 # 初始化连接,连接池连接对象数量 initialSize=10 #最大连接数 maxActive=30 #超时等待时间(毫秒为单位) maxWait=3000
3.3目录结构
3.4具体代码
3.4.1dao层(com.nanchu.dao)
AccountDao接口
package com.nanchu.dao;
import com.nanchu.entity.Account;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 10:08
* @Version 1.0
*/
public interface AccountDao {
//添加
int accountInsert(Account account);
//删除
int accountDelete(int atid);
//修改
int accountUpdate(Account account);
//查询单个
Account accountSelectOne(int atid);
//查询所有
List<Account> accountSelectAll();
}
ClazzDao接口
package com.nanchu.dao;
import com.nanchu.entity.Clazz;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 10:09
* @Version 1.0
*/
public interface ClazzDao {
//添加
int clazzInsert(Clazz clazz);
//删除
int clazzDelete(int clazzid);
//修改
int clazzUpdate(Clazz clazz);
//查询单个
Clazz clazzSelectOne(int clazzid);
//查询所有
List<Clazz> clazzSelectAll();
}
CourseDao接口
package com.nanchu.dao;
import com.nanchu.entity.Course;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 10:10
* @Version 1.0
*/
public interface CourseDao {
//添加
int courseInsert(Course course);
//删除
int courseDelete(int courseid);
//修改
int courseUpdate(Course course);
//查询单个
Course courseSelectOne(int courseid);
//查询所有
List<Course> courseSelectAll();
}
ScoreDao接口
package com.nanchu.dao;
import com.nanchu.entity.Account;
import com.nanchu.entity.Score;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 10:11
* @Version 1.0
*/
public interface ScoreDao {
//添加
int scoreInsert(Score score);
//删除
int scoreDelete(int studentid);
//修改
int scoreUpdate(Score score);
//查询单个学生全部成绩
List<Score> scoreSelectOne(int studentid);
//查询所有
List<Score> scoreSelectAll();
// #查看自己指定科目成绩信息
Score selectStudentCourse(int studentid,int courseid);
}
StudentDao接口
package com.nanchu.dao;
import com.nanchu.entity.Student;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 10:11
* @Version 1.0
*/
public interface StudentDao {
//添加
int studentInsert(Student student);
//删除
int studentDelete(int studentid);
//修改
int studentUpdate(Student student);
//查询单个
Student studentSelectOne(int studentid);
//查询所有
List<Student> studentSelectAll();
}
TeacherDao接口
package com.nanchu.dao;
import com.nanchu.entity.Teacher;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 10:11
* @Version 1.0
*/
public interface TeacherDao {
//添加
int teacherInsert(Teacher teacher);
//删除
int teacherDelete(int teacherid);
//修改
int teacherUpdate(Teacher teacher);
//查询单个
Teacher teacherSelectOne(int teacherid);
//查询所有
List<Teacher> teacherSelectAll();
}
QueryOtherWay接口
package com.nanchu.dao;
import com.nanchu.entity.Score;
import com.nanchu.entity.Student;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/11 8:28
* @Version 1.0
*/
public interface QueryOtherWay {
/**
*#查看自己班级所有学生信息
*
*/
List<Student> allStudentClazz(int clazzid);
/**
* 查看自己班级指定学生信息
*/
List<Student> OneStudentClazz(int clazzid,int studentid);
/**
* #查看所有班级指定科目成绩信息
*/
List<Score> allClazzOneCourseScore(int courseid);
/**
*自己班级所有学生所有科目成绩信息
* @return
*/
List<Score> allStudentAllCourseScore(int clazzid);
/**
*自己班级所有学生指定科目成绩信息
* @return
*/
List<Score> allStudentOneCourseScore(int clazzid,int courseid);
/**
*自己班级指定学生所有科目成绩信息
* @return
*/
List<Score> oneStudentAllCourseScore(int clazzid,int studentid);
/**
* 自己班级指定学生指定科目成绩信息
*
*/
List<Score> oneStudentOneCourseScore(int clazzid,int studentid,int courseid);
}
3.4.2dao.imlp层(com.nanchu.dao.impl)
AccountDaoImpl实现类
package com.nanchu.dao.impl;
import com.nanchu.dao.AccountDao;
import com.nanchu.entity.Account;
import com.nanchu.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 10:09
* @Version 1.0
*/
public class AccountDaoImpl implements AccountDao {
// 创建QueryRunner对象,并传递一个数据源对象
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
@Override
public int accountInsert(Account account) {
return 0;
}
@Override
public int accountDelete(int atid) {
return 0;
}
@Override
public int accountUpdate(Account account) {
return 0;
}
@Override
public Account accountSelectOne(int atid) {
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanHandler<Account> account = new BeanHandler<Account>(Account.class);
String sql = "SELECT * FROM `account` WHERE `atid`=?;";
try {
return queryRunner.query(sql, account, atid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public List<Account> accountSelectAll() {
return null;
}
}
ClazzDaoImpl实现类
package com.nanchu.dao.impl;
import com.nanchu.dao.ClazzDao;
import com.nanchu.entity.Clazz;
import com.nanchu.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 12:24
* @Version 1.0
*/
public class ClazzDaoImpl implements ClazzDao {
// 创建QueryRunner对象,并传递一个数据源对象
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
// 插入
@Override
public int clazzInsert(Clazz clazz) {
/*
* #添加班级信息
INSERT INTO clazz(clazzid,clname) VALUE(14,'4班');*/
String sql = "INSERT INTO `clazz`(`clazzid`,`clname`)VALUES(?,?);";
Object[] args = {clazz.getClazzid(), clazz.getClname()};
try {
return queryRunner.update(sql, args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
//删除
@Override
public int clazzDelete(int clazzid) {
/*
#删除指定班级信息
DELETE FROM clazz WHERE clazzid=14;
* */
String sql = "DELETE FROM `clazz` WHERE `clazzid` = ?;";
try {
return queryRunner.update(sql, clazzid);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
//修改
@Override
public int clazzUpdate(Clazz clazz) {
/*
* #修改指定班级信息
SELECT * FROM clazz;
UPDATE clazz SET clname = '1班' WHERE clazzid = 11;
UPDATE clazz SET clname = '5班' WHERE clazzid = 11;
* */
String sql = "UPDATE `clazz` SET `clname` = ? WHERE `clazzid`=?;";
Object[] args = { clazz.getClname(),clazz.getClazzid()};
try {
return queryRunner.update(sql, args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
//查询单个
@Override
public Clazz clazzSelectOne(int clazzid) {
/*
#查看指定班级信息
SELECT * FROM clazz WHERE clazzid = 11;
* */
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanHandler<Clazz> clazz = new BeanHandler<Clazz>(Clazz.class);
String sql = "SELECT * FROM `clazz` WHERE `clazzid`=?;";
try {
return queryRunner.query(sql, clazz, clazzid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//查询所有
@Override
public List<Clazz> clazzSelectAll() {
/*
#查看所有班级信息
SELECT * FROM clazz;
* */
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Clazz> clazzList = new BeanListHandler<Clazz>(Clazz.class);
String sql = "SELECT * FROM `clazz`;";
try {
return queryRunner.query(sql, clazzList);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
CourseDaoImpl实现类
package com.nanchu.dao.impl;
import com.nanchu.dao.CourseDao;
import com.nanchu.entity.Course;
import com.nanchu.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 12:42
* @Version 1.0
*/
public class CourseDaoImpl implements CourseDao {
// 创建QueryRunner对象,并传递一个数据源对象
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
@Override
public int courseInsert(Course course) {
/*
#添加课程信息
INSERT INTO course(courseid,csname,teacherid) VALUE (407,'数学',206);
* */
String sql = "INSERT INTO `course`(`courseid`,`csname`,`teacherid`)VALUES(?,?,?);";
Object[] args = { course.getCourseid(),course.getCsname(),course.getTeacherid() };
try {
return queryRunner.update(sql, args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int courseDelete(int courseid) {
/*
* #删除指定课程信息
DELETE FROM course WHERE courseid=407;*/
String sql = "DELETE FROM `course` WHERE `courseid` = ?;";
try {
return queryRunner.update(sql, courseid);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
// 修改
@Override
public int courseUpdate(Course course) {
/* #修改指定课程信息
SELECT * FROM course;
UPDATE course SET csname = 'Python',teacherid = 201 WHERE courseid = 401;
UPDATE course SET csname = '语文' , teacherid = 201 WHERE courseid = 401;*/
String sql = "UPDATE `course` SET `csname` = ?,`teacherid`=?WHERE `courseid`=?;";
Object[] args = { course.getCsname(),course.getTeacherid(),course.getCourseid() };
try {
return queryRunner.update(sql, args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public Course courseSelectOne(int courseid) {
/*
#查看指定课程信息
SELECT * FROM course WHERE courseid=401;
* */
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanHandler<Course> course = new BeanHandler<Course>(Course.class);
String sql = "SELECT * FROM `course` WHERE `courseid`=?;";
try {
return queryRunner.query(sql, course, courseid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public List<Course> courseSelectAll() {
/*
#查看所有课程信息
SELECT * FROM course;
* */
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Course> courseList = new BeanListHandler<Course>(Course.class);
String sql = "SELECT * FROM `course`;";
try {
return queryRunner.query(sql, courseList);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
ScoreDaoImpl实现类
package com.nanchu.dao.impl;
import com.nanchu.dao.ScoreDao;
import com.nanchu.entity.Score;
import com.nanchu.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:03
* @Version 1.0
*/
public class ScoreDaoImpl implements ScoreDao {
// 创建QueryRunner对象,并传递一个数据源对象
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
@Override
public int scoreInsert(Score score) {
return 0;
}
@Override
public int scoreDelete(int studentid) {
return 0;
}
@Override
public int scoreUpdate(Score score) {
return 0;
}
// 查看一个学生全部成绩
@Override
public List<Score> scoreSelectOne(int studentid) {
/*
#查看指定学生成绩信息
SE SELECT * FROM score WHERE studentid=301;
* */
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Score> scoreList = new BeanListHandler<Score>(Score.class);
String sql = "SELECT * FROM `score` WHERE `studentid`=?;";
try {
return queryRunner.query(sql,scoreList,studentid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
// 查询所有学生所有成绩。
@Override
public List<Score> scoreSelectAll() {
/*
#查看所有学生成绩信息
SELECT * FROM score;
* */
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Score> scoreList = new BeanListHandler<Score>(Score.class);
String sql = "SELECT * FROM `score`;";
try {
return queryRunner.query(sql, scoreList);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
// #查看自己指定科目成绩信息
@Override
public Score selectStudentCourse(int studentid,int courseid){
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanHandler<Score> sc = new BeanHandler<Score>(Score.class);
/*SELECT * FROM score WHERE courseid=401 AND studentid=301;*/
String sql = "SELECT * FROM `score` WHERE `studentid`=? AND `courseid`=?;";
try {
return queryRunner.query(sql, sc, studentid,courseid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
StudentDaoImpl实现类
package com.nanchu.dao.impl;
import com.nanchu.dao.StudentDao;
import com.nanchu.entity.Student;
import com.nanchu.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:09
* @Version 1.0
*/
public class StudentDaoImpl implements StudentDao {
// 创建QueryRunner对象,并传递一个数据源对象
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
@Override
public int studentInsert(Student student) {
/*
#添加学生信息
INSERT INTO student(studentid,stname,stsex,stage,clazzid,stpassword) VALUES(311,'学生55','女',14,13,'st008');
* */
String sql = "INSERT INTO `student`(`studentid`,`stname`,`stsex`,`stage`,`clazzid`,`stpassword`)VALUES(?,?,?,?,?,?);";
Object[] args = { student.getStudentid(), student.getStname(),student.getStsex(),
student.getStage(),student.getClazzid(),student.getStpassword() };
try {
return queryRunner.update(sql, args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int studentDelete(int studentid) {
/*
#删除指定学生信息
DELETE FROM student WHERE studentid=311;
* */
String sql = "DELETE FROM `student` WHERE `studentid` = ?;";
try {
queryRunner.update("SET FOREIGN_KEY_CHECKS = 0;");
int x= queryRunner.update(sql, studentid);
queryRunner.update("SET FOREIGN_KEY_CHECKS = 1; ");
return x;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int studentUpdate(Student student) {
/*
#修改指定学生信息
UPDATE student SET stname='学生1',stsex='男',stage=15,clazzid=11,stpassword='st001' WHERE studentid = 301;
UPDATE student SET stname='我是学生',stsex='男',stage=15,clazzid=11,stpassword='st001' WHERE studentid = 301;
*/
String sql = "UPDATE `student` SET `stname` = ?,`stsex`=?,`stage`=? ,`clazzid` = ?,`stpassword`=? WHERE `studentid`=?;";
Object[] args = { student.getStname(),student.getStsex(),student.getStage(),
student.getClazzid(),student.getStpassword(),student.getStudentid()
};
try {
return queryRunner.update(sql, args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public Student studentSelectOne(int studentid) {
/*#查看指定学生信息
SELECT * FROM student WHERE studentid=301;*/
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanHandler<Student> student = new BeanHandler<Student>(Student.class);
String sql = "SELECT * FROM `student` WHERE `studentid`=?;";
try {
return queryRunner.query(sql, student, studentid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public List<Student> studentSelectAll() {
/*#查看所有学生信息
SELECT * FROM student;*/
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Student> studentList = new BeanListHandler<Student>(Student.class);
String sql = "SELECT * FROM `student`;";
try {
return queryRunner.query(sql, studentList);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
TeacherDaoImpl实现类
package com.nanchu.dao.impl;
import com.nanchu.dao.TeacherDao;
import com.nanchu.entity.Teacher;
import com.nanchu.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:24
* @Version 1.0
*/
public class TeacherDaoImpl implements TeacherDao {
// 创建QueryRunner对象,并传递一个数据源对象
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
@Override
public int teacherInsert(Teacher teacher) {
/*#添加老师信息
INSERT INTO teacher(teacherid,thname,thage,thsex,clazzid,thpassword)
VALUES(207,'老师7',40,'男',13,'8888');*/
String sql = "INSERT INTO `teacher`(`teacherid`,`thname`,`thage`,`thsex`,`clazzid`,`thpassword`)VALUES(?,?,?,?,?,?);";
Object[] args = {
teacher.getTeacherid(),teacher.getThname(),teacher.getThage(),
teacher.getThsex(),teacher.getClazzid(),teacher.getThpassword()
};
try {
return queryRunner.update(sql, args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int teacherDelete(int teacherid) {
/*#删除指定老师信息
DELETE FROM teacher WHERE teacherid=207;*/
String sql = "DELETE FROM `teacher` WHERE `teacherid` = ?;";
try {
queryRunner.update("SET FOREIGN_KEY_CHECKS = 0;");
int x = queryRunner.update(sql, teacherid);
queryRunner.update("SET FOREIGN_KEY_CHECKS = 1;");
return x;
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public int teacherUpdate(Teacher teacher) {
/* #修改指定老师信息
UPDATE teacher SET thname='老师1',thage=30,thsex='男',clazzid=11,thpassword='th001' WHERE teacherid = 201;
UPDATE teacher SET thname='老师8',thage=30,thsex='女',clazzid=11,thpassword='th001' WHERE teacherid = 201;*/
String sql = "UPDATE `teacher` SET `thname` = ?,`thage`=?,`thsex`=?,`clazzid` = ?,`thpassword`=? WHERE `teacherid`=?;";
Object[] args = {
teacher.getThname(),teacher.getThage(),teacher.getThsex(),
teacher.getClazzid(),teacher.getThpassword(), teacher.getTeacherid()
};
try {
return queryRunner.update(sql, args);
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public Teacher teacherSelectOne(int teacherid) {
/*#查看指定老师信息
SELECT * FROM teacher WHERE teacherid =201 ;*/
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanHandler<Teacher> teacher = new BeanHandler<Teacher>(Teacher.class);
String sql = "SELECT * FROM `teacher` WHERE `teacherid`=?;";
try {
return queryRunner.query(sql, teacher, teacherid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
@Override
public List<Teacher> teacherSelectAll() {
/*#查看所有老师信息
SELECT * FROM teacher ;*/
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Teacher> teacherList = new BeanListHandler<Teacher>(Teacher.class);
String sql = "SELECT * FROM `teacher`;";
try {
return queryRunner.query(sql, teacherList);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
QueryOtherWayImpl实现类
package com.nanchu.dao.impl;
import com.nanchu.dao.QueryOtherWay;
import com.nanchu.entity.Clazz;
import com.nanchu.entity.Score;
import com.nanchu.entity.Student;
import com.nanchu.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.SQLException;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/11 9:14
* @Version 1.0
*/
public class QueryOtherWayImpl implements QueryOtherWay{
// 创建QueryRunner对象,并传递一个数据源对象
private QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());
/**
* #查看自己班级所有学生信息
*
* @param clazzid
*/
@Override
public List<Student> allStudentClazz(int clazzid) {
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Student> studentList = new BeanListHandler<Student>(Student.class);
//SELECT *
//FROM student
//WHERE clazzid=11;
String sql="SELECT * FROM student WHERE `clazzid`=?;";
try {
return queryRunner.query(sql,studentList,clazzid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 查看自己班级指定学生信息
*
* @param studentid
*/
@Override
public List<Student> OneStudentClazz(int clazzid,int studentid) {
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Student> studentList = new BeanListHandler<Student>(Student.class);
//SELECT *
//FROM student
//WHERE clazzid = 11 AND studentid=301;
String sql="SELECT * FROM student WHERE `clazzid`=? AND `studentid`=?;";
try {
return queryRunner.query(sql,studentList,clazzid,studentid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* #查看所有班级指定科目成绩信息
*
* @param courseid
*/
@Override
public List<Score> allClazzOneCourseScore(int courseid) {
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Score> scoreList = new BeanListHandler<Score>(Score.class);
/*
SELECT b.`studentid`,b.`courseid`,b.`grade`
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND b.`courseid`=401;
* */
String sql="SELECT b.`studentid`,b.`courseid`,b.`grade` FROM `clazz` a ,`score` b,`student` c WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND b.`courseid`=?;";
try {
return queryRunner.query(sql,scoreList,courseid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 自己班级所有学生所有科目成绩信息
*
* @param clazzid
* @return
*/
@Override
public List<Score> allStudentAllCourseScore(int clazzid) {
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Score> scoreList = new BeanListHandler<Score>(Score.class);
String sql="SELECT b.`studentid`,b.`courseid`,b.`grade` FROM `student` a ,`score` b,`course` c WHERE a.`studentid` = b.`studentid` AND b.`courseid` = c.`courseid` AND a.`clazzid`=?;";
try {
return queryRunner.query(sql,scoreList,clazzid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 自己班级所有学生指定科目成绩信息
*
* @param clazzid
* @param courseid
* @return
*/
@Override
public List<Score> allStudentOneCourseScore(int clazzid, int courseid) {
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Score> scoreList = new BeanListHandler<Score>(Score.class);
/*
* SELECT b.studentid,b.`courseid`,b.`grade`
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND b.`courseid`=401 AND a.`clazzid`=11 ;
*/
String sql="SELECT b.`studentid`,b.`courseid`,b.`grade` FROM `clazz` a ,`score` b,`student` c WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND a.`clazzid`=? AND b.`courseid`=?;";
try {
return queryRunner.query(sql,scoreList,clazzid,courseid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 自己班级指定学生所有科目成绩信息
*
* @param clazzid
* @param studentid
* @return
*/
@Override
public List<Score> oneStudentAllCourseScore(int clazzid, int studentid) {
// 查询一个数据,使用BeanHandler将记录转换为对象
/*
* SELECT b.studentid,b.`courseid`,b.`grade`
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND c.`studentid`=301 AND a.`clazzid`=11 ;
* */
BeanListHandler<Score> scoreList = new BeanListHandler<Score>(Score.class);
String sql="SELECT b.`studentid`,b.`courseid`,b.`grade` FROM `clazz` a ,`score` b,`student` c WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND a.`clazzid`= ? AND c.`studentid`=?;";
try {
return queryRunner.query(sql,scoreList,clazzid,studentid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
/**
* 自己班级指定学生指定科目成绩信息
*
* @param clazzid
* @param studentid
* @param courseid
*/
@Override
public List<Score> oneStudentOneCourseScore(int clazzid, int studentid, int courseid) {
// 查询一个数据,使用BeanHandler将记录转换为对象
BeanListHandler<Score> scoreList = new BeanListHandler<Score>(Score.class);
/*
* SELECT b.studentid,b.`courseid`,b.`grade`
FROM clazz a,score b ,student c
WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND a.`clazzid`=11 AND c.`studentid`=301 AND b.`courseid`=401 ;
* */
String sql="SELECT b.`studentid`,b.`courseid`,b.`grade` FROM `clazz` a ,`score` b,`student` c WHERE a.`clazzid`=c.`clazzid` AND b.`studentid`=c.`studentid` AND a.`clazzid`= ? AND c.`studentid`=? AND b.`courseid`=? ;";
try {
return queryRunner.query(sql,scoreList,clazzid,studentid,courseid);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
3.4.3service层(com.nanchu.service)
AccountService接口
package com.nanchu.service;
import com.nanchu.entity.Account;
/**
* @Author 南初
* @Create 2024/3/10 12:13
* @Version 1.0
*/
public interface AccountService {
// 查询账号信息
//查询单个
Account accountSelectOneAccount(int atid);
}
ClazzService接口
package com.nanchu.service;
import com.nanchu.entity.Clazz;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 12:54
* @Version 1.0
*/
public interface ClazzService {
/**
* 增加一个班级
* */
//增加
int addClazz(Clazz clazz);
/**
* 删除一个班级
* */
//删除
int deleteClazz(int clazzid);
/**
* 修改一个班级信息
* */
//修改
int updateClazz(Clazz clazz);
/**
* 查询一个班级信息
* */
//查询单个
Clazz selectOneClazz(int clazzid);
/**
* 查询所有班级信息
* */
//查询所有
List<Clazz> selectAllClazz();
}
CourseService接口
package com.nanchu.service;
import com.nanchu.entity.Course;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:37
* @Version 1.0
*/
public interface CourseService {
/**
* 增加一个课程
* */
//增加
int addCourse(Course course);
/**
* 删除一个课程
* */
//删除
int deleteCourse(int courseid);
/**
* 修改一个课程信息
* */
//修改
int updateCourse(Course course);
/**
* 查询一个课程信息
* */
//查询单个
Course selectOneCourse(int courseid);
/**
* 查询所有课程信息
* */
//查询所有
List<Course> selectAllCourse();
}
ScoreService接口
package com.nanchu.service;
import com.nanchu.entity.Score;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:37
* @Version 1.0
*/
public interface ScoreService {
/**
* 查询一个成绩信息
* */
//查询单个
List<Score> selectOneScore(int studentid);
/**
* 查询所有成绩信息
* */
//查询所有
List<Score> selectAllScore();
// 查询自己指定科目成绩
Score selectStudentCourse(int studentid,int courseid);
}
StudentService接口
package com.nanchu.service;
import com.nanchu.entity.Clazz;
import com.nanchu.entity.Course;
import com.nanchu.entity.Student;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:37
* @Version 1.0
*/
public interface StudentService {
/**
* 增加一个学生
* */
//增加
int addStudent(Student student);
/**
* 删除一个学生
* */
//删除
int deleteStudent(int studentid);
/**
* 修改一个学生信息
* */
//修改
int updateStudent(Student student);
/**
* 查询一个学生信息
* */
//查询单个
Student selectOneStudent(int studentid);
/**
* 查询所有学生信息
* */
//查询所有
List<Student> selectAllStudent();
}
TeacherService接口
package com.nanchu.service;
import com.nanchu.entity.Clazz;
import com.nanchu.entity.Student;
import com.nanchu.entity.Teacher;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:38
* @Version 1.0
*/
public interface TeacherService {
/**
* 增加一个老师
* */
//增加
int addTeacher(Teacher teacher);
/**
* 删除一个老师
* */
//删除
int deleteTeacher(int teacherid);
/**
* 修改一个老师信息
* */
//修改
int updateTeacher(Teacher teacher);
/**
* 查询一个老师信息
* */
//查询单个
Teacher selectOneTeacher(int teacherid);
/**
* 查询所有老师信息
* */
//查询所有
List<Teacher> selectAllTeacher();
}
3.4.4service.impl(com.nanchu.service.impl)
AccountServiceImpl实现类
package com.nanchu.service.impl;
import com.nanchu.dao.AccountDao;
import com.nanchu.dao.impl.AccountDaoImpl;
import com.nanchu.entity.Account;
import com.nanchu.service.AccountService;
/**
* @Author 南初
* @Create 2024/3/10 12:14
* @Version 1.0
*/
public class AccountServiceImpl implements AccountService {
AccountDao accountDao = new AccountDaoImpl();
@Override
public Account accountSelectOneAccount(int atid) {
Account account =accountDao.accountSelectOne(atid);
if(account!=null){
return account;
}else{
System.out.println("没有你要查找管理员账号,查找失败");
}
return null;
}
}
ClazzServiceImpl实现类
package com.nanchu.service.impl;
import com.nanchu.dao.ClazzDao;
import com.nanchu.dao.impl.ClazzDaoImpl;
import com.nanchu.entity.Clazz;
import com.nanchu.service.ClazzService;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 12:58
* @Version 1.0
*/
public class ClazzServiceImpl implements ClazzService {
ClazzDao clazzDao = new ClazzDaoImpl();
/**
* 增加一个班级
* @param clazz
*/
@Override
public int addClazz(Clazz clazz) {
// 查询添加的班级是否存在
Clazz cl = clazzDao.clazzSelectOne(clazz.getClazzid());
if (cl == null) {
return clazzDao.clazzInsert(clazz);
} else {
System.out.println("班级已经存在,不能重复添加");
}
return 0;
}
/**
* 删除一个班级
*
* @param clazzid
*/
@Override
public int deleteClazz(int clazzid) {
// 查询添加的班级是否存在
Clazz cl = clazzDao.clazzSelectOne(clazzid);
if (cl != null) {
return clazzDao.clazzDelete(clazzid);
} else {
System.out.println("班级不存在,不能删除");
}
return 0;
}
/**
* 修改一个班级信息
*
* @param clazz
*/
@Override
public int updateClazz(Clazz clazz) {
// 查询添加的商品是否存在
Clazz cl = clazzDao.clazzSelectOne(clazz.getClazzid());
if (cl!= null) {
return clazzDao.clazzUpdate(clazz);
} else {
System.out.println("班级不存在,不能修改");
}
return 0;
}
/**
* 查询一个班级信息
*
* @param clazzid
*/
@Override
public Clazz selectOneClazz(int clazzid) {
Clazz clazz = clazzDao.clazzSelectOne(clazzid);
if(clazz!=null){
return clazz;
}else{
System.out.println("没有你要查找产品,查找失败");
}
return null;
}
/**
* 查询所有班级信息
*/
@Override
public List<Clazz> selectAllClazz() {
List<Clazz> clazzList = clazzDao.clazzSelectAll();
if(clazzList.size()!=0){
return clazzList;
}else{
System.out.println("数据库为空,没有班级信息");
}
return null;
}
}
CourseServiceImpl实现类
package com.nanchu.service.impl;
import com.nanchu.dao.CourseDao;
import com.nanchu.dao.impl.CourseDaoImpl;
import com.nanchu.entity.Clazz;
import com.nanchu.entity.Course;
import com.nanchu.service.CourseService;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:52
* @Version 1.0
*/
public class CourseServiceImpl implements CourseService {
CourseDao courseDao = new CourseDaoImpl();
/**
* 增加一个课程
*
* @param course
*/
@Override
public int addCourse(Course course) {
// 查询添加的课程是否存在
Course cs = courseDao.courseSelectOne(course.getCourseid());
if (cs == null) {
return courseDao.courseInsert(course);
} else {
System.out.println("课程已经存在,不能重复添加");
}
return 0;
}
/**
* 删除一个课程
*
* @param courseid
*/
@Override
public int deleteCourse(int courseid) {
// 查询添加的课程是否存在
Course cs = courseDao.courseSelectOne(courseid);
if (cs != null) {
return courseDao.courseDelete(courseid);
} else {
System.out.println("课程不存在,不能删除");
}
return 0;
}
/**
* 修改一个课程信息
*
* @param course
*/
@Override
public int updateCourse(Course course) {
// 查询添加的课程是否存在
Course cs = courseDao.courseSelectOne(course.getCourseid());
if (cs!= null) {
return courseDao.courseUpdate(course);
} else {
System.out.println("课程不存在,不能修改");
}
return 0;
}
/**
* 查询一个课程信息
*
* @param courseid
*/
@Override
public Course selectOneCourse(int courseid) {
Course cs = courseDao.courseSelectOne(courseid);
if(cs!=null){
return cs;
}else{
System.out.println("没有你要查找课程,查找失败");
}
return null;
}
/**
* 查询所有课程信息
*/
@Override
public List<Course> selectAllCourse() {
List<Course> courseList = courseDao.courseSelectAll();
if(courseList.size()!=0){
return courseList;
}else{
System.out.println("数据库为空,没有课程");
}
return null;
}
}
ScoreServiceImpl实现类
package com.nanchu.service.impl;
import com.nanchu.dao.ScoreDao;
import com.nanchu.dao.impl.ScoreDaoImpl;
import com.nanchu.entity.Clazz;
import com.nanchu.entity.Score;
import com.nanchu.service.ScoreService;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:53
* @Version 1.0
*/
public class ScoreServiceImpl implements ScoreService {
ScoreDao scoreDao = new ScoreDaoImpl();
/**
* 查询一个成绩信息
*
* @param studentid
*/
@Override
public List<Score> selectOneScore(int studentid) {
List<Score> scoreList = scoreDao.scoreSelectOne(studentid);
if(scoreList.size()!=0){
return scoreList;
}else{
System.out.println("数据库为空,没有成绩");
}
return null;
}
/**
* 查询所有成绩信息
*/
@Override
public List<Score> selectAllScore() {
List<Score> scoreList = scoreDao.scoreSelectAll();
if(scoreList.size()!=0){
return scoreList;
}else{
System.out.println("数据库为空,没有成绩");
}
return null;
}
/**
* 查询自己指定科目成绩
* @param studentid
* @param courseid
* @return
*/
@Override
public Score selectStudentCourse(int studentid, int courseid) {
Score score = scoreDao.selectStudentCourse(studentid, courseid);
if(score!=null){
return score;
}else{
System.out.println("没有你要查找成绩,查找失败");
}
return null;
}
}
StudentServiceImpl实现类
package com.nanchu.service.impl;
import com.nanchu.dao.StudentDao;
import com.nanchu.dao.impl.StudentDaoImpl;
import com.nanchu.entity.Student;
import com.nanchu.service.StudentService;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:53
* @Version 1.0
*/
public class StudentServiceImpl implements StudentService {
StudentDao studentDao = new StudentDaoImpl();
/**
* 增加一个学生
*
* @param student
*/
@Override
public int addStudent(Student student) {
// 查询添加的学生是否存在
Student st = studentDao.studentSelectOne(student.getStudentid());
if (st == null) {
return studentDao.studentInsert(student);
} else {
System.out.println("该学生已经存在,不能重复添加");
}
return 0;
}
/**
* 删除一个学生
*
* @param studentid
*/
@Override
public int deleteStudent(int studentid) {
// 查询添加的商品是否存在
Student student = studentDao.studentSelectOne(studentid);
if (student != null) {
return studentDao.studentDelete(studentid);
} else {
System.out.println("学生不存在,不能删除");
}
return 0;
}
/**
* 修改一个学生信息
*
* @param student
*/
@Override
public int updateStudent(Student student) {
// 查询添加的商品是否存在
Student st = studentDao.studentSelectOne(student.getStudentid());
if (st!= null) {
return studentDao.studentUpdate(student);
} else {
System.out.println("学生不存在,不能修改");
}
return 0;
}
/**
* 查询一个学生信息
*
* @param studentid
*/
@Override
public Student selectOneStudent(int studentid) {
Student student = studentDao.studentSelectOne(studentid);
if(student!=null){
return student;
}else{
System.out.println("没有你要查找学生信息,查找失败");
}
return null;
}
/**
* 查询所有学生信息
*/
@Override
public List<Student> selectAllStudent() {
List<Student> studentList = studentDao.studentSelectAll();
if(studentList.size()!=0){
return studentList;
}else{
System.out.println("学生表为空,没有学生信息");
}
return null;
}
}
TeacherServiceImpl实现类
package com.nanchu.service.impl;
import com.nanchu.dao.TeacherDao;
import com.nanchu.dao.impl.TeacherDaoImpl;
import com.nanchu.entity.Teacher;
import com.nanchu.service.TeacherService;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/10 14:53
* @Version 1.0
*/
public class TeacherServiceImpl implements TeacherService {
TeacherDao teacherDao = new TeacherDaoImpl();
/**
* 增加一个老师
*
* @param teacher
*/
@Override
public int addTeacher(Teacher teacher) {
// 查询添加的商品是否存在
Teacher th = teacherDao.teacherSelectOne(teacher.getTeacherid());
if (th == null) {
return teacherDao.teacherInsert(teacher);
} else {
System.out.println("老师已经存在,不能重复添加");
}
return 0;
}
/**
* 删除一个老师
*
* @param teacherid
*/
@Override
public int deleteTeacher(int teacherid) {
// 查询添加的商品是否存在
Teacher teacher = teacherDao.teacherSelectOne(teacherid);
if (teacher != null) {
return teacherDao.teacherDelete(teacherid);
} else {
System.out.println("老师不存在,不能删除");
}
return 0;
}
/**
* 修改一个老师信息
*
* @param teacher
*/
@Override
public int updateTeacher(Teacher teacher) {
// 查询添加的商品是否存在
Teacher th = teacherDao.teacherSelectOne(teacher.getTeacherid());
if (th!= null) {
return teacherDao.teacherUpdate(teacher);
} else {
System.out.println("老师信息不存在,不能修改");
}
return 0;
}
/**
* 查询一个老师信息
*
* @param teacherid
*/
@Override
public Teacher selectOneTeacher(int teacherid) {
Teacher teacher = teacherDao.teacherSelectOne(teacherid);
if(teacher!=null){
return teacher;
}else{
System.out.println("没有你要查找老师信息,查找失败");
}
return null;
}
/**
* 查询所有老师信息
*/
@Override
public List<Teacher> selectAllTeacher() {
List<Teacher> teacherList = teacherDao.teacherSelectAll();
if(teacherList.size()!=0){
return teacherList;
}else{
System.out.println("老师表为空,没有老师信息");
}
return null;
}
}
3.4.5entity实体类
Account实体类
package com.nanchu.entity;
public class Account {
private int atid;
private String atname;
private String atpassword;
public Account(){
}
public Account(String atname,String atpassword){
this.atname=atname;
this.atpassword=atpassword;
}
public Account(int atid,String atname,String atpassword){
this.atid=atid;
this.atname=atname;
this.atpassword=atpassword;
}
public void setAtid(int atid){
this.atid=atid;
}
public int getAtid(){
return atid;
}
public void setAtname(String atname){
this.atname=atname;
}
public String getAtname(){
return atname;
}
public void setAtpassword(String atpassword){
this.atpassword=atpassword;
}
public String getAtpassword(){
return atpassword;
}
@Override
public String toString() {
return "Account{"+"atid="+atid+",atname="+atname+",atpassword="+atpassword+"}";
}
}
Clazz实体类
package com.nanchu.entity;
public class Clazz {
private int clazzid;
private String clname;
public Clazz(){
}
public Clazz(String clname){
this.clname=clname;
}
public Clazz(int clazzid,String clname){
this.clazzid=clazzid;
this.clname=clname;
}
public void setClazzid(int clazzid){
this.clazzid=clazzid;
}
public int getClazzid(){
return clazzid;
}
public void setClname(String clname){
this.clname=clname;
}
public String getClname(){
return clname;
}
@Override
public String toString() {
return "Clazz{"+"clazzid="+clazzid+",clname="+clname+"}";
}
}
Course实体类
package com.nanchu.entity;
public class Course {
private int courseid;
private String csname;
private int teacherid;
public Course(){
}
public Course(String csname,int teacherid){
this.csname=csname;
this.teacherid=teacherid;
}
public Course(int courseid,String csname,int teacherid){
this.courseid=courseid;
this.csname=csname;
this.teacherid=teacherid;
}
public void setCourseid(int courseid){
this.courseid=courseid;
}
public int getCourseid(){
return courseid;
}
public void setCsname(String csname){
this.csname=csname;
}
public String getCsname(){
return csname;
}
public void setTeacherid(int teacherid){
this.teacherid=teacherid;
}
public int getTeacherid(){
return teacherid;
}
@Override
public String toString() {
return "Course{"+"courseid="+courseid+",csname="+csname+",teacherid="+teacherid+"}";
}
}
Score实体类
package com.nanchu.entity;
public class Score {
private int studentid;
private int courseid;
private int grade;
public Score(){
}
public Score(int courseid,int grade){
this.courseid=courseid;
this.grade=grade;
}
public Score(int studentid,int courseid,int grade){
this.studentid=studentid;
this.courseid=courseid;
this.grade=grade;
}
public void setStudentid(int studentid){
this.studentid=studentid;
}
public int getStudentid(){
return studentid;
}
public void setCourseid(int courseid){
this.courseid=courseid;
}
public int getCourseid(){
return courseid;
}
public void setGrade(int grade){
this.grade=grade;
}
public int getGrade(){
return grade;
}
@Override
public String toString() {
return "Score{"+"studentid="+studentid+",courseid="+courseid+",grade="+grade+"}";
}
}
Student实体类
package com.nanchu.entity;
public class Student {
private int studentid;
private String stname;
private String stsex;
private int stage;
private int clazzid;
private String stpassword;
public Student(){
}
public Student(String stname,String stsex,int stage,int clazzid,String stpassword){
this.stname=stname;
this.stsex=stsex;
this.stage=stage;
this.clazzid=clazzid;
this.stpassword=stpassword;
}
public Student(int studentid,String stname,String stsex,int stage,int clazzid,String stpassword){
this.studentid=studentid;
this.stname=stname;
this.stsex=stsex;
this.stage=stage;
this.clazzid=clazzid;
this.stpassword=stpassword;
}
public void setStudentid(int studentid){
this.studentid=studentid;
}
public int getStudentid(){
return studentid;
}
public void setStname(String stname){
this.stname=stname;
}
public String getStname(){
return stname;
}
public void setStsex(String stsex){
this.stsex=stsex;
}
public String getStsex(){
return stsex;
}
public void setStage(int stage){
this.stage=stage;
}
public int getStage(){
return stage;
}
public void setClazzid(int clazzid){
this.clazzid=clazzid;
}
public int getClazzid(){
return clazzid;
}
public void setStpassword(String stpassword){
this.stpassword=stpassword;
}
public String getStpassword(){
return stpassword;
}
@Override
public String toString() {
return "Student{"+"studentid="+studentid+",stname="+stname+",stsex="+stsex+",stage="+stage+",clazzid="+clazzid+",stpassword="+stpassword+"}";
}
}
Teacher实体类
package com.nanchu.entity;
public class Teacher {
private int teacherid;
private String thname;
private int thage;
private String thsex;
private int clazzid;
private String thpassword;
public Teacher(){
}
public Teacher(String thname,int thage,String thsex,int clazzid,String thpassword){
this.thname=thname;
this.thage=thage;
this.thsex=thsex;
this.clazzid=clazzid;
this.thpassword=thpassword;
}
public Teacher(int teacherid,String thname,int thage,String thsex,int clazzid,String thpassword){
this.teacherid=teacherid;
this.thname=thname;
this.thage=thage;
this.thsex=thsex;
this.clazzid=clazzid;
this.thpassword=thpassword;
}
public void setTeacherid(int teacherid){
this.teacherid=teacherid;
}
public int getTeacherid(){
return teacherid;
}
public void setThname(String thname){
this.thname=thname;
}
public String getThname(){
return thname;
}
public void setThage(int thage){
this.thage=thage;
}
public int getThage(){
return thage;
}
public void setThsex(String thsex){
this.thsex=thsex;
}
public String getThsex(){
return thsex;
}
public void setClazzid(int clazzid){
this.clazzid=clazzid;
}
public int getClazzid(){
return clazzid;
}
public void setThpassword(String thpassword){
this.thpassword=thpassword;
}
public String getThpassword(){
return thpassword;
}
@Override
public String toString() {
return "Teacher{"+"teacherid="+teacherid+",thname="+thname+",thage="+thage+",thsex="+thsex+",clazzid="+clazzid+",thpassword="+thpassword+"}";
}
}
3.4.6utils工具类
PageUtils选择功能页面
package com.nanchu.utils;
/**
* @Author 南初
* @Create 2024/3/10 17:17
* @Version 1.0
*/
public class PageUtils {
public void page1(){
System.out.println("----------------登录界面----------------");
System.out.println(" 1、管理员登录");
System.out.println(" 2、教师登录");
System.out.println(" 3、学生登录");
System.out.println(" 0、退出");
}
public void page2(){
System.out.println("****************欢迎进入管理员账户页面****************");
System.out.println(" 1、查看所有班级信息");
System.out.println(" 2、查看指定班级信息");
System.out.println(" 3、修改指定班级信息");
System.out.println(" 4、添加班级信息");
System.out.println(" 5、删除指定班级信息");
System.out.println("---------------------------------");
System.out.println(" 6、查看所有课程信息");
System.out.println(" 7、查看指定课程信息");
System.out.println(" 8、修改指定课程信息");
System.out.println(" 9、添加课程信息");
System.out.println(" 10、删除指定课程信息");
System.out.println("---------------------------------");
System.out.println(" 11、查看所有老师信息");
System.out.println(" 12、查看指定老师信息");
System.out.println(" 13、修改指定老师信息");
System.out.println(" 14、删除指定老师信息");
System.out.println(" 15、添加老师信息");
System.out.println("---------------------------------");
System.out.println(" 16、查看所有学生信息");
System.out.println(" 17、查看指定学生信息");
System.out.println(" 18、添加学生信息");
System.out.println(" 19、修改指定学生信息");
System.out.println(" 20、删除指定学生信息");
System.out.println("---------------------------------");
System.out.println(" 21、查看所有学生成绩信息");
System.out.println(" 22、查看指定学生成绩信息");
System.out.println(" 23、查看指定班级所有学生成绩信息");
System.out.println(" 24、查看所有班级指定科目成绩信息");
System.out.println(" 25、查看指定班级指定科目成绩信息");
System.out.println(" 0、退出");
System.out.println("---------------------------------");
}
public void page3(){
System.out.println("**************欢迎进入老师账户页面****************");
System.out.println(" 1、 查看自己的信息");
System.out.println(" 2、 修改自己的信息");
System.out.println(" 3、 删除自己的信息");
System.out.println("-----------------------------------------------");
System.out.println(" 4、 查看自己班级所有学生信息");
System.out.println(" 5、 查看自己班级指定学生信息");
System.out.println(" 6、 添加自己班级学生信息");
System.out.println(" 7、 修改自己班级指定学生信息");
System.out.println(" 8、 删除自己班级指定学生信息");
System.out.println("------------------------------------------------");
System.out.println(" 9、 查看自己班级所有学生所有科目成绩信息");
System.out.println(" 10、查看自己班级所有学生指定科目成绩信息");
System.out.println(" 11、查看自己班级指定学生所有科目成绩信息");
System.out.println(" 12、查看自己班级指定学生指定科目成绩信息");
System.out.println(" 0、退出");
System.out.println("--------------------------------------------------");
}
public void page4(){
System.out.println("***************欢迎进入学生账户页面****************");
System.out.println(" 1、查看自己的信息");
System.out.println(" 2、修改自己的信息");
System.out.println("-----------------------------------------------");
System.out.println(" 3、删除自己的信息");
System.out.println(" 4、查看自己所有科目成绩信息");
System.out.println(" 5、查看自己指定科目成绩信息");
System.out.println("-----------------------------------------------");
System.out.println(" 6、查看自己班级所有学生所有科目成绩信息");
System.out.println(" 7、查看自己班级所有学生指定科目成绩信息");
System.out.println(" 8、查看自己班级指定学生所有科目成绩信息");
System.out.println(" 9、查看自己班级指定学生指定科目成绩信息");
System.out.println(" 0、退出");
System.out.println("------------------------------------------------");
}
}
LoginUtils三种账号登录验证
package com.nanchu.utils;
import com.nanchu.entity.Account;
import com.nanchu.entity.Student;
import com.nanchu.entity.Teacher;
import com.nanchu.service.AccountService;
import com.nanchu.service.StudentService;
import com.nanchu.service.TeacherService;
import com.nanchu.service.impl.AccountServiceImpl;
import com.nanchu.service.impl.StudentServiceImpl;
import com.nanchu.service.impl.TeacherServiceImpl;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 15:44
* @Version 1.0
*/
public class LoginUtils {
Scanner scan = new Scanner(System.in);
public int loginCheck1() {
AccountService accountService = new AccountServiceImpl();
// 管理员登录验证 手机号码+密码
System.out.println("\n---请输入你的验证信息---");
System.out.print("\n请输入管理员账号:");
int accountId = scan.nextInt();
System.out.print("\n请输入账号密码:");
String accountPassword = scan.next();
Account account =null;
account = accountService.accountSelectOneAccount(accountId);
while(true){
if(account == null){
System.out.println("输入账号信息错误,请重新输入信息!");
System.out.print("\n请输入管理员账号:");
accountId = scan.nextInt();
System.out.print("\n请输入账号密码:");
accountPassword = scan.next();
account = accountService.accountSelectOneAccount(accountId);
}else if( accountId == account.getAtid() && accountPassword.equals(account.getAtpassword())){
break;
}else{
System.out.println("输入账号信息错误,请重新输入信息!");
System.out.print("\n请输入管理员账号:");
accountId = scan.nextInt();
System.out.print("\n请输入账号密码:");
accountPassword = scan.next();
account = accountService.accountSelectOneAccount(accountId);
}
}
System.out.println("登录成功!");
return accountId;
}
public int loginCheck2() {
TeacherService teacherService = new TeacherServiceImpl();
// 教师登录验证 手机号码+密码
System.out.println("\n---请输入你的验证信息---");
System.out.print("\n请输入教师账号:");
int teacherId = scan.nextInt();
System.out.print("\n请输入账号密码:");
String teacherPassword = scan.next();
Teacher teacher =null;
teacher = teacherService.selectOneTeacher(teacherId);
while(true){
if(teacher == null){
System.out.println("输入账号信息错误,请重新输入信息!");
System.out.print("\n请输入教师账号:");
teacherId = scan.nextInt();
System.out.print("\n请输入账号密码:");
teacherPassword = scan.next();
teacher = teacherService.selectOneTeacher(teacherId);
}else if( teacherId == teacher.getTeacherid() && teacherPassword.equals(teacher.getThpassword())){
break;
}else {
System.out.println("输入账号信息错误,请重新输入信息!");
System.out.print("\n请输入教师账号:");
teacherId = scan.nextInt();
System.out.print("\n请输入账号密码:");
teacherPassword = scan.next();
teacher = teacherService.selectOneTeacher(teacherId);
}
}
System.out.println("登录成功!");
return teacherId;
}
public int loginCheck3() {
StudentService studentService = new StudentServiceImpl();
// 管理员登录验证 手机号码+密码
System.out.println("\n---请输入你的验证信息---");
System.out.print("\n请输入学生账号:");
int studentId = scan.nextInt();
System.out.print("\n请输入账号密码:");
String studentPassword = scan.next();
Student student =null;
student = studentService.selectOneStudent(studentId);
while(true){
if(student == null){
System.out.println("输入账号信息错误,请重新输入信息!");
System.out.print("\n请输入学生账号:");
studentId = scan.nextInt();
System.out.print("\n请输入账号密码:");
studentPassword = scan.next();
student = studentService.selectOneStudent(studentId);
}else if( studentId == student.getStudentid() && studentPassword.equals(student.getStpassword())){
break;
}else {
System.out.println("输入账号信息错误,请重新输入信息!");
System.out.print("\n请输入学生账号:");
studentId = scan.nextInt();
System.out.print("\n请输入账号密码:");
studentPassword = scan.next();
student = studentService.selectOneStudent(studentId);
}
}
System.out.println("登录成功!");
return studentId;
}
}
DBUtils:Druid连接池的连接
package com.nanchu.utils;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
/**
* @Author 南初
* @Create 2024/3/9 21:55
* @Version 1.0
*/
public class DBUtils {
// 声明一个连接池对象
private static DruidDataSource druidDataSource;
static {
// 实例化配置文件对象
Properties properties = new Properties();
try {
// 加载配置文件内容
InputStream is = DBUtils.class
.getResourceAsStream("/database.properties");
properties.load(is);
// 创建连接池
druidDataSource = (DruidDataSource) DruidDataSourceFactory
.createDataSource(properties);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
//返回一个数据源
public static DataSource getDataSource(){
return druidDataSource;
}
}
ClazzFunctionUtils:Clazz班级的相关操作直接在这里被封装成为一个方法直接调用即可
package com.nanchu.utils;
import com.nanchu.entity.Clazz;
import com.nanchu.service.ClazzService;
import com.nanchu.service.impl.ClazzServiceImpl;
import java.util.List;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 20:04
* @Version 1.0
*/
public class ClazzFunctionUtils {
Scanner scan = new Scanner(System.in);
// 增加班级
public void addOneClazz() {
//创建ClazzService引用,指向ClazzServiceImpl实现类
ClazzService clazzService = new ClazzServiceImpl();
//增加班级
System.out.println("请输入要增加的班级编号:");
int clazzid = scan.nextInt();
System.out.println("请输入要增加的班级名称:");
String clazzname = scan.next();
Clazz clazz = new Clazz(clazzid,clazzname);
int result = clazzService.addClazz(clazz);
String str = result==1?"班级添加成功":"班级添加失败";
System.out.println(str);
}
// 修改班级信息
public void alterClazzInfo() {
//创建ClazzService引用,指向ClazzServiceImpl实现类
ClazzService clazzService = new ClazzServiceImpl();
//修改班级
System.out.println("请输入要修改的班级编号:");
int clazzid = scan.nextInt();
System.out.println("请输入要修改的班级名称:");
String clazzname = scan.next();
Clazz clazz = new Clazz(clazzid,clazzname);
int result = clazzService.updateClazz(clazz);
String str = result == 1 ? "修改成功" : "修改失败";
System.out.println(str);
}
// 删除班级
public void delectClazz() {
//创建ClazzService引用,指向ClazzServiceImpl实现类
ClazzService clazzService = new ClazzServiceImpl();
//删除班级
System.out.println("请输入要删除的班级编号:");
int clazzid = scan.nextInt();
int result = clazzService.deleteClazz(clazzid);
String str = result == 1 ? "删除成功" : "删除失败";
System.out.println(str);
}
//查询一个班级信息
public void lookOneClazz() {
//创建ClazzService引用,指向ClazzServiceImpl实现类
ClazzService clazzService = new ClazzServiceImpl();
//查询班级
System.out.println("请输入要查询的班级编号:");
int clazzid = scan.nextInt();
Clazz clazz = clazzService.selectOneClazz(clazzid);
if (clazz != null) {
System.out.println(clazz);
} else {
System.out.println("你要查询的班级不存在");
}
}
//查询所有班级信息
public void lookAllClazz() {
//创建ClazzService引用,指向ClazzServiceImpl实现类
ClazzService clazzService = new ClazzServiceImpl();
List<Clazz> clazzList = clazzService.selectAllClazz();
for (int i = 0; i < clazzList.size(); i++) {
System.out.println(clazzList.get(i));
}
}
}
CourseFunctionUtils:Course课程的相关操作直接在这里被封装成为一个方法直接调用即可
package com.nanchu.utils;
import com.nanchu.entity.Course;
import com.nanchu.service.CourseService;
import com.nanchu.service.impl.CourseServiceImpl;
import java.util.List;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 20:48
* @Version 1.0
*/
public class CourseFunctionUtils {
Scanner scan = new Scanner(System.in);
//增加课程
public void addCourseInfo(){
//创建CourseService引用,指向CourseServiceImpl实现类
CourseService courseService = new CourseServiceImpl();
System.out.println("请输入要增加课程的编号:");
int courseid = scan.nextInt();
System.out.println("请输入要增加课程的名称:");
String name = scan.next();
System.out.println("请输入要增加此课程所带教师编号:");
int teacherid = scan.nextInt();
Course course = new Course(courseid,name,teacherid);
int result = courseService.addCourse(course);
String str = result==1?"课程添加成功":"课程添加失败";
System.out.println(str);
}
//修改课程
public void alertCourseInfo(){
//创建CourseService引用,指向CourseServiceImpl实现类
CourseService courseService = new CourseServiceImpl();
System.out.println("请输入要修改课程的编号:");
int courseid = scan.nextInt();
System.out.println("请输入要修改课程的名称:");
String name = scan.next();
System.out.println("请输入要修改此课程所带教师编号:");
int teacherid = scan.nextInt();
Course course = new Course(courseid,name,teacherid);
int result = courseService.updateCourse(course);
String str = result == 1 ? "修改成功" : "修改失败";
System.out.println(str);
}
//删除课程
public void delectCourseInfo(){
//创建CourseService引用,指向CourseServiceImpl实现类
CourseService courseService = new CourseServiceImpl();
System.out.println("请输入要删除课程的编号:");
int courseid = scan.nextInt();
int result = courseService.deleteCourse(courseid);
String str = result == 1 ? "删除成功" : "删除失败";
System.out.println(str);
}
//查询一个课程
public void lookOneCourse(){
//创建CourseService引用,指向CourseServiceImpl实现类
CourseService courseService = new CourseServiceImpl();
System.out.println("请输入要查询课程的编号:");
int courseid = scan.nextInt();
Course course = courseService.selectOneCourse(courseid);
if (course != null) {
System.out.println(course);
} else {
System.out.println("你要查询的课程不存在");
}
}
//查询所有课程
public void lookAllCourse(){
//创建CourseService引用,指向CourseServiceImpl实现类
CourseService courseService = new CourseServiceImpl();
List<Course> courseList = courseService.selectAllCourse();
for (int i = 0; i < courseList.size(); i++) {
System.out.println(courseList.get(i));
}
}
}
ScoreFunctionUtils:Score课程分数的相关操作直接在这里被封装成为一个方法直接调用即可
package com.nanchu.utils;
import com.nanchu.entity.Score;
import com.nanchu.service.ScoreService;
import com.nanchu.service.impl.ScoreServiceImpl;
import java.util.List;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 21:03
* @Version 1.0
*/
public class ScoreFunctionUtils {
Scanner scan = new Scanner(System.in);
// 查询一位同学成绩
public void lookScoreOneStudent(int studentid){
// 创建ProductService引用,指向ProductServiceImpl实现类
ScoreService scoreService = new ScoreServiceImpl();
List<Score> scoreList= scoreService.selectOneScore(studentid);
for (int i = 0; i < scoreList.size(); i++) {
System.out.println(scoreList.get(i));
}
}
//查询自己指定科目成绩
public void lookScoreOneStudent(int studentid, int courseid){
// 创建ProductService引用,指向ProductServiceImpl实现类
ScoreService scoreService = new ScoreServiceImpl();
Score score = scoreService.selectStudentCourse(studentid, courseid);
if (score != null) {
System.out.println(score);
} else {
System.out.println("你要查询的成绩不存在");
}
}
// 查询所有同学成绩
public void lookScoreAllStudent(){
// 创建ProductService引用,指向ProductServiceImpl实现类
ScoreService scoreService = new ScoreServiceImpl();
List<Score> scoreList = scoreService.selectAllScore();
for (int i = 0; i < scoreList.size(); i++) {
System.out.println(scoreList.get(i));
}
}
}
StudentFunctionUtils:Student学生的相关操作直接在这里被封装成为一个方法直接调用即可
package com.nanchu.utils;
import com.nanchu.entity.Student;
import com.nanchu.service.StudentService;
import com.nanchu.service.impl.StudentServiceImpl;
import java.util.List;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 19:11
* @Version 1.0
*/
public class StudentFunctionUtils {
Scanner scan = new Scanner(System.in);
// 查看学生的信息
public void lookOwnInfo(int studentid){
// 创建StudentService引用,指向StudentServiceImpl实现类
StudentService studentService = new StudentServiceImpl();
Student student = studentService.selectOneStudent(studentid);
if (student != null) {
System.out.println(student);
} else {
System.out.println("你要查询的学生信息不存在");
}
}
// 查看学生的信息
public Student returnOneStudent(int studentid){
// 创建StudentService引用,指向StudentServiceImpl实现类
StudentService studentService = new StudentServiceImpl();
Student student = studentService.selectOneStudent(studentid);
if (student != null) {
return student;
} else {
System.out.println("你要查询的学生信息不存在");
}
return null;
}
// 查看所有学生的信息
public void lookAllInfo(){
// 创建StudentService引用,指向StudentServiceImpl实现类
StudentService studentService = new StudentServiceImpl();
List<Student> studentList = studentService.selectAllStudent();
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i));
}
}
// 修改学生的信息
public void alterOwnInfo(int studentid){
// 创建StudentService引用,指向StudentServiceImpl实现类
StudentService studentService = new StudentServiceImpl();
/* UPDATE student SET stname='我是学生',stsex='男',stage=15,clazzid=11,stpassword='st001'
WHERE studentid = 301;*/
System.out.println("请输入要修改的姓名:");
String name = scan.next();
System.out.println("请输入要修改的性别:");
String sex = scan.next();
System.out.println("请输入要修改的年龄:");
int age = scan.nextInt();
System.out.println("请输入要修改的班级:");
int clazz = scan.nextInt();
System.out.println("请输入要修改的密码:");
String password = scan.next();
Student student = new Student(studentid,name,sex, age, clazz, password);
int result =studentService.updateStudent(student);
String str = result == 1 ? "修改成功" : "修改失败";
System.out.println(str);
}
// 删除学生的信息
public void delectOwninfo(int studentid){
// 创建StudentService引用,指向StudentServiceImpl实现类
StudentService studentService = new StudentServiceImpl();
int result = studentService.deleteStudent(studentid);
String str = result == 1 ? "删除成功" : "删除失败";
System.out.println(str);
}
// 增加学生信息
public void addInfo(){
// 创建StudentService引用,指向StudentServiceImpl实现类
StudentService studentService = new StudentServiceImpl();
System.out.println("请输入要增加学生的账号:");
int studentid = scan.nextInt();
System.out.println("请输入要增加学生的姓名:");
String name = scan.next();
System.out.println("请输入要增加学生的性别:");
String sex = scan.next();
System.out.println("请输入要增加学生的年龄:");
int age = scan.nextInt();
System.out.println("请输入要增加学生的班级:");
int clazz = scan.nextInt();
System.out.println("请输入要增加学生的密码:");
String password = scan.next();
Student student = new Student(studentid,name,sex, age, clazz, password);
//增加学生信息;
int result =studentService.addStudent(student);
String str = result==1?"学生信息添加成功":"学生信息添加失败";
System.out.println(str);
}
// 增加学生信息
public void TeacherAddStudentInfo(int clazzid){
// 创建StudentService引用,指向StudentServiceImpl实现类
StudentService studentService = new StudentServiceImpl();
System.out.println("请输入要增加学生的账号:");
int studentid = scan.nextInt();
System.out.println("请输入要增加学生的姓名:");
String name = scan.next();
System.out.println("请输入要增加学生的性别:");
String sex = scan.next();
System.out.println("请输入要增加学生的年龄:");
int age = scan.nextInt();
System.out.println("请输入要增加学生的密码:");
String password = scan.next();
Student student = new Student(studentid,name,sex, age, clazzid, password);
//增加学生信息;
int result =studentService.addStudent(student);
String str = result==1?"学生信息添加成功":"学生信息添加失败";
System.out.println(str);
}
}
TeacherFunctionUtils:Teacher教师的相关操作直接在这里被封装成为一个方法直接调用即可
package com.nanchu.utils;
import com.nanchu.entity.Teacher;
import com.nanchu.service.TeacherService;
import com.nanchu.service.impl.TeacherServiceImpl;
import java.util.List;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 20:14
* @Version 1.0
*/
public class TeacherFunctionUtils {
Scanner scan = new Scanner(System.in);
// 增加老师信息
public void addTeacherInfo(){
//创建TeacherService引用,指向TeacherServiceImpl实现类
TeacherService teacherService = new TeacherServiceImpl();
//增加老师信息
System.out.println("请输入要增加教师的账号:");
int teacherid = scan.nextInt();
System.out.println("请输入要增加教师的姓名:");
String name = scan.next();
System.out.println("请输入要增加教师的年龄:");
int age = scan.nextInt();
System.out.println("请输入要增加教师的性别:");
String sex = scan.next();
System.out.println("请输入要增加教师所带的班级:");
int clazz = scan.nextInt();
System.out.println("请输入要增加教师的账号密码:");
String password = scan.next();
Teacher teacher = new Teacher(teacherid,name,age,sex,clazz,password);
int result = teacherService.addTeacher(teacher);
String str = result==1?"教师信息添加成功":"教师信息添加失败";
System.out.println(str);
}
//删除老师
public void delectTeacherInfo(int teacherid){
//创建TeacherService引用,指向TeacherServiceImpl实现类
TeacherService teacherService = new TeacherServiceImpl();
//删除班级
int result = teacherService.deleteTeacher(teacherid);
String str = result == 1 ? "删除成功" : "删除失败";
System.out.println(str);
}
// 修改老师信息
public void alertTeacherInfo(int teacherid){
//创建TeacherService引用,指向TeacherServiceImpl实现类
TeacherService teacherService = new TeacherServiceImpl();
//修改老师信息
System.out.println("请输入要修改教师的姓名:");
String name = scan.next();
System.out.println("请输入要修改教师的年龄:");
int age = scan.nextInt();
System.out.println("请输入要修改教师的性别:");
String sex = scan.next();
System.out.println("请输入要修改教师所带的班级:");
int clazz = scan.nextInt();
System.out.println("请输入要修改教师的账号密码:");
String password = scan.next();
Teacher teacher = new Teacher(teacherid,name,age,sex,clazz,password);
int result =teacherService.updateTeacher(teacher);
String str = result == 1 ? "修改成功" : "修改失败";
System.out.println(str);
}
// 查询一个老师信息
public void lookOneTeacher(int teacherid ){
//创建TeacherService引用,指向TeacherServiceImpl实现类
TeacherService teacherService = new TeacherServiceImpl();
Teacher teacher = teacherService.selectOneTeacher(teacherid);
if (teacher != null) {
System.out.println(teacher);
} else {
System.out.println("你要查询的教师信息不存在");
}
}
// 查询一个老师信息
public Teacher returnOneTeacher(int teacherid ){
//创建TeacherService引用,指向TeacherServiceImpl实现类
TeacherService teacherService = new TeacherServiceImpl();
Teacher teacher = teacherService.selectOneTeacher(teacherid);
if (teacher != null) {
return teacher;
} else {
System.out.println("你要查询的教师信息不存在");
}
return null;
}
//查询所有教师信息
public void lookAllTeacher(){
//创建TeacherService引用,指向TeacherServiceImpl实现类
TeacherService teacherService = new TeacherServiceImpl();
List<Teacher> teacherList = teacherService.selectAllTeacher();
for (int i = 0; i < teacherList.size(); i++) {
System.out.println(teacherList.get(i));
}
}
}
QueryOtherWayFunctionUtils:这里定义的是多表查询的操作,实现了dao层,没有编写service层,相关操作直接在这里被封装成为一个方法直接调用即可
package com.nanchu.utils;
import com.nanchu.dao.QueryOtherWay;
import com.nanchu.dao.impl.QueryOtherWayImpl;
import com.nanchu.entity.Score;
import com.nanchu.entity.Student;
import org.junit.jupiter.api.Test;
import java.util.List;
/**
* @Author 南初
* @Create 2024/3/12 14:08
* @Version 1.0
*/
public class QueryOtherWayFunctionUtils {
// 查看自己班级所有学生信息
public void AllStudentClazz(int clazzid) {
QueryOtherWay queryOtherWay = new QueryOtherWayImpl();
List<Student> studentList = queryOtherWay.allStudentClazz(clazzid) ;
if(studentList.size()!=0){
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i));
}
}else{
System.out.println("数据库为空,没有成绩");
}
}
// 查看自己班级指定学生信息
public void TeacherOneStudentClazz(int clazzid,int studentid) {
QueryOtherWay queryOtherWay = new QueryOtherWayImpl();
List<Student> studentList = queryOtherWay.OneStudentClazz(clazzid,studentid) ;
if(studentList.size()!=0){
for (int i = 0; i < studentList.size(); i++) {
System.out.println(studentList.get(i));
}
}else{
System.out.println("数据库为空,没有成绩");
}
}
//自己班级所有学生所有科目成绩信息
public void AllStudentAllCourseScore(int clazzid){
QueryOtherWay queryOtherWay = new QueryOtherWayImpl();
List<Score> scoreList = queryOtherWay.allStudentAllCourseScore(clazzid);
if(scoreList.size()!=0){
for (int i = 0; i < scoreList.size(); i++) {
System.out.println(scoreList.get(i));
}
}else{
System.out.println("数据库为空,没有成绩");
}
}
//#查看自己班级所有学生指定科目成绩信息
public void AllStudentOneCourseScore(int clazzid,int courseid){
QueryOtherWay queryOtherWay = new QueryOtherWayImpl();
List<Score> scoreList = queryOtherWay.allStudentOneCourseScore(clazzid,courseid);
if(scoreList.size()!=0){
for (int i = 0; i < scoreList.size(); i++) {
System.out.println(scoreList.get(i));
}
}else{
System.out.println("数据库为空,没有成绩");
}
}
// oneStudentAllCourseScore
public void OneStudentAllCourseScore(int clazzid,int studentid){
QueryOtherWay queryOtherWay = new QueryOtherWayImpl();
List<Score> scoreList = queryOtherWay.oneStudentAllCourseScore(clazzid,studentid);
if(scoreList.size()!=0){
for (int i = 0; i < scoreList.size(); i++) {
System.out.println(scoreList.get(i));
}
}else{
System.out.println("数据库为空,没有成绩");
}
}
//oneStudentOneCourseScore
@Test
public void OneStudentOneCourseScore(int clazzid,int studentid,int courseid){
QueryOtherWay queryOtherWay = new QueryOtherWayImpl();
List<Score> scoreList = queryOtherWay.oneStudentOneCourseScore(clazzid,studentid,courseid);
if(scoreList.size()!=0){
for (int i = 0; i < scoreList.size(); i++) {
System.out.println(scoreList.get(i));
}
}else{
System.out.println("数据库为空,没有成绩");
}
}
//#查看所有班级指定科目成绩信息
public void AllClazzOneCourseScore(int courseid){
QueryOtherWay queryOtherWay = new QueryOtherWayImpl();
List<Score> scoreList = queryOtherWay.allClazzOneCourseScore(courseid);
if(scoreList.size()!=0){
for (int i = 0; i < scoreList.size(); i++) {
System.out.println(scoreList.get(i));
}
}else{
System.out.println("数据库为空,没有成绩");
}
}
}
3.4.7test测试类
AccountFunctionTest:管理员功能在这里面实现了,后面在view中的EMSystem_main主方法中直接调用AccountFunctionTest,省点事,就没有把AccountFunction管理员功能封装在view中
package com.nanchu.test;
import com.nanchu.utils.*;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 23:56
* @Version 1.0
*/
public class AccountFunctionTest {
public static void accountLogin(){
Scanner scan = new Scanner(System.in);
LoginUtils loginUtils =new LoginUtils();
PageUtils pageUtils= new PageUtils();
ClazzFunctionUtils clazz = new ClazzFunctionUtils();
CourseFunctionUtils course = new CourseFunctionUtils();
TeacherFunctionUtils teacher = new TeacherFunctionUtils();
StudentFunctionUtils student = new StudentFunctionUtils();
ScoreFunctionUtils score = new ScoreFunctionUtils();
QueryOtherWayFunctionUtils qOther = new QueryOtherWayFunctionUtils();
int accountid = loginUtils.loginCheck1();
while(true){
pageUtils.page2();
System.out.print("请输入功能选项(只能输入0~25):");
int StudentFunctionId = scan.nextInt();
if(StudentFunctionId == 1){ // 查看所有班级信息
clazz.lookAllClazz();
}else if (StudentFunctionId == 2){ // 查看指定班级信息
clazz.lookOneClazz();
}else if (StudentFunctionId == 3){ // 修改指定班级信息
clazz.alterClazzInfo();
}else if (StudentFunctionId == 4){ // 添加班级信息
clazz.addOneClazz();
}else if (StudentFunctionId == 5){ // 删除指定班级信息
clazz.delectClazz();
}else if (StudentFunctionId == 6){ // 查看所有课程信息
course.lookAllCourse();
}else if (StudentFunctionId == 7){ // 查看指定课程信息
course.lookOneCourse();
}else if (StudentFunctionId == 8){ // 修改指定课程信息
course.alertCourseInfo();
}else if (StudentFunctionId == 9){ // 添加课程信息
course.addCourseInfo();
}else if (StudentFunctionId == 10){ // 删除指定课程信息
course.delectCourseInfo();
}else if (StudentFunctionId == 11){ // 查看所有老师信息
teacher.lookAllTeacher();
}else if (StudentFunctionId == 12){ // 查看指定老师信息
System.out.print("请输入你要查看老师的编号:");
int teacherid = scan.nextInt();
teacher.lookOneTeacher(teacherid);
}else if (StudentFunctionId == 13){ // 修改指定老师信息
System.out.print("请输入你要修改老师的编号:");
int teacherid = scan.nextInt();
teacher.alertTeacherInfo(teacherid);
}else if (StudentFunctionId == 14){ // 删除指定老师信息
System.out.print("请输入你要删除老师的编号:");
int teacherid = scan.nextInt();
teacher.delectTeacherInfo(teacherid);
}else if (StudentFunctionId == 15){ // 添加老师信息
teacher.addTeacherInfo();
}else if (StudentFunctionId == 16){ // 查看所有学生信息
student.lookAllInfo();
}else if (StudentFunctionId == 17){ // 查看指定学生信息
System.out.print("请输入你要查看学生的编号:");
int studentid = scan.nextInt();
student.lookOwnInfo(studentid);
}else if (StudentFunctionId == 18){ // 添加学生信息
student.addInfo();
}else if (StudentFunctionId == 19){ // 修改指定学生信息
System.out.print("请输入你要修改学生的编号:");
int studentid = scan.nextInt();
student.alterOwnInfo(studentid);
}else if (StudentFunctionId == 20){ // 删除指定学生信息
System.out.print("请输入你要删除学生的编号:");
int studentid = scan.nextInt();
student.delectOwninfo(studentid);
}else if (StudentFunctionId == 21){ // 查看所有学生成绩信息
score.lookScoreAllStudent();
}else if (StudentFunctionId == 22){ // 查看指定学生成绩信息
System.out.print("请输入你要查看指定学生成绩信息的同学的编号:");
int studentid = scan.nextInt();
score.lookScoreOneStudent(studentid);
}else if (StudentFunctionId == 23){ // 查看指定班级所有学生成绩信息
System.out.print("请输入你要查询的班级编号:");
int clazzid = scan.nextInt();
qOther.AllStudentAllCourseScore(clazzid);
}else if (StudentFunctionId == 24){ // 查看所有班级指定科目成绩信息
System.out.print("请输入指定科目编号:");
int courseid = scan.nextInt();
qOther.AllClazzOneCourseScore(courseid);
}else if (StudentFunctionId == 25){ // 查看指定班级指定科目成绩信息
System.out.print("请输入指定班级编号:");
int clazzid = scan.nextInt();
System.out.print("请输入指定科目编号:");
int courseid = scan.nextInt();
qOther.AllStudentOneCourseScore(clazzid,courseid);
}else{
break;
}
}
}
}
TeacherFunctionTest这里同AccountFunctionTest一样
package com.nanchu.test;
import com.nanchu.entity.Student;
import com.nanchu.entity.Teacher;
import com.nanchu.utils.*;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 23:47
* @Version 1.0
*/
public class TeacherFunctionTest {
public static void teacherLogin() {
Scanner scan = new Scanner(System.in);
LoginUtils loginUtils = new LoginUtils();
PageUtils pageUtils = new PageUtils();
StudentFunctionUtils stu = new StudentFunctionUtils();
TeacherFunctionUtils tfu = new TeacherFunctionUtils();
QueryOtherWayFunctionUtils qOther = new QueryOtherWayFunctionUtils();
int teacherid = loginUtils.loginCheck2();
Teacher teacher = tfu.returnOneTeacher(teacherid);
int clazzid = teacher.getClazzid();
while (true) {
pageUtils.page3();
System.out.print("请输入你想要查询的功能(只能输入1~9):");
int StudentFunctionId = scan.nextInt();
if (StudentFunctionId == 1) { // 查看自己的信息
tfu.lookOneTeacher(teacherid);
} else if (StudentFunctionId == 2) { // 修改自己的信息
tfu.alertTeacherInfo(teacherid);
} else if (StudentFunctionId == 3) { // 删除自己的信息
tfu.delectTeacherInfo(teacherid);
break;
} else if (StudentFunctionId == 4) { // 查看自己班级所有学生信息
qOther.AllStudentClazz(clazzid);
} else if (StudentFunctionId == 5) { // 查看自己班级指定学生信息
System.out.print("请输入你要查看指定学生编号:");
int stuid = scan.nextInt();
qOther.TeacherOneStudentClazz(clazzid,stuid);
} else if (StudentFunctionId == 6) { // 添加自己班级学生信息
stu.TeacherAddStudentInfo(clazzid);
} else if (StudentFunctionId == 7) { // 修改自己班级指定学生信息
System.out.print("请输入学生编号:");
int stuid = scan.nextInt();
Student student = stu.returnOneStudent(stuid);
int stuclazz = student.getClazzid();
if(clazzid == stuclazz){
stu.alterOwnInfo(stuid);
}else {
System.out.println("你输入的学生不是您班级的学生,你无法删除此学生信息!");
}
} else if (StudentFunctionId == 8) { // 删除自己班级指定学生信息
System.out.print("请输入学生编号:");
int stuid = scan.nextInt();
Student student = stu.returnOneStudent(stuid);
int stuclazz = student.getClazzid();
if(clazzid == stuclazz){
stu.delectOwninfo(stuid);
}else {
System.out.println("你输入的学生不是您班级的学生,你无法删除此学生信息!");
}
} else if (StudentFunctionId == 9) { // 查看自己班级所有学生所有科目成绩信息
qOther.AllStudentAllCourseScore(clazzid);
}else if (StudentFunctionId == 10) { // 查看自己班级所有学生指定科目成绩信息
System.out.print("请输入指定科目编号:");
int courseid = scan.nextInt();
qOther.AllStudentOneCourseScore(clazzid,courseid);
}else if (StudentFunctionId == 11) { // 查看自己班级指定学生所有科目成绩信息
System.out.print("请输入学生编号:");
int stuid = scan.nextInt();
qOther.OneStudentAllCourseScore(clazzid,stuid);
}else if (StudentFunctionId == 12) { // 查看自己班级指定学生指定科目成绩信息
System.out.print("请输入你要查看指定学生编号:");
int stuid = scan.nextInt();
System.out.print("请输入指定科目编号:");
int courseid = scan.nextInt();
qOther.OneStudentOneCourseScore(clazzid,stuid,courseid);
}else {
break;
}
}
}
}
StudentFunctionTest这里同AccountFunctionTest一样
package com.nanchu.test;
import com.nanchu.entity.Student;
import com.nanchu.utils.*;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 18:51
* @Version 1.0
*/
public class StudentFunctionTest {
public static void studentLogin() {
Scanner scan = new Scanner(System.in);
LoginUtils loginUtils =new LoginUtils();
PageUtils pageUtils= new PageUtils();
ScoreFunctionUtils sfu = new ScoreFunctionUtils();
StudentFunctionUtils stu = new StudentFunctionUtils();
QueryOtherWayFunctionUtils qOther = new QueryOtherWayFunctionUtils();
int studentid = loginUtils.loginCheck3();
while(true){
pageUtils.page4();
System.out.print("请输入功能选项(只能输入0~9):");
int StudentFunctionId = scan.nextInt();
if(StudentFunctionId == 1){ // 查看自己的信息
stu.lookOwnInfo(studentid);
}else if (StudentFunctionId == 2){ // 修改自己的信息
stu.alterOwnInfo(studentid);
}else if (StudentFunctionId == 3){ // 删除自己的信息
stu.delectOwninfo(studentid);
break;
}else if (StudentFunctionId == 4){ // 查看自己所有科目成绩信息
sfu.lookScoreOneStudent(studentid);
}else if (StudentFunctionId == 5){ // 查看自己指定科目成绩信息
System.out.print("请输入你要查询的科目编号:");
int courseid = scan.nextInt();
sfu.lookScoreOneStudent(studentid,courseid);
}else if (StudentFunctionId == 6){ // 查看自己班级所有学生所有科目成绩信息
Student student = stu.returnOneStudent(studentid);
int clazzid = student.getClazzid();
qOther.AllStudentAllCourseScore(clazzid);
}else if (StudentFunctionId == 7){ // 查看自己班级所有学生指定科目成绩信息
Student student = stu.returnOneStudent(studentid);
int clazzid = student.getClazzid();
System.out.print("请输入你要查看的自己班级所有学生指定科目编号:");
int courseid = scan.nextInt();
qOther.AllStudentOneCourseScore(clazzid,courseid);
}else if (StudentFunctionId == 8){ // 查看自己班级指定学生所有科目成绩信息
Student student = stu.returnOneStudent(studentid);
int clazzid = student.getClazzid();
System.out.print("请输入你要查看自己班级指定学生编号:");
int stuid = scan.nextInt();
qOther.OneStudentAllCourseScore(clazzid,stuid);
}else if (StudentFunctionId == 9){ // 查看自己班级指定学生指定科目成绩信息
Student student = stu.returnOneStudent(studentid);
int clazzid = student.getClazzid();
System.out.print("请输入你要查看指定学生编号:");
int stuid = scan.nextInt();
System.out.print("请输入指定科目编号:");
int courseid = scan.nextInt();
qOther.OneStudentOneCourseScore(clazzid,stuid,courseid);
}else{
break;
}
}
}
}
3.4.8view层
EMSystem_main:所有功能最后在这里展现
package com.nanchu.view;
import com.nanchu.test.AccountFunctionTest;
import com.nanchu.test.StudentFunctionTest;
import com.nanchu.test.TeacherFunctionTest;
import com.nanchu.utils.PageUtils;
import java.util.Scanner;
/**
* @Author 南初
* @Create 2024/3/10 15:41
* @Version 1.0
*/
public class EMSystem_main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
PageUtils pageUtils = new PageUtils();
int identityid;
while(true){
pageUtils.page1();
System.out.print("请选择你要登录的身份(只能输入1,2,3,0):");
identityid = scan.nextInt();
// 校验输入的序号,只能是0到3
while (true) {
if (identityid == 1 || identityid == 2 || identityid == 3 || identityid == 0) {
break;
} else {
System.out.print("输入错误,请重新输入(只能输入1,2,3,0):");
identityid = scan.nextInt();
}
}
if(identityid == 1){ // 管理员登录
AccountFunctionTest.accountLogin();
}else if (identityid == 2){ // 教师登录
TeacherFunctionTest.teacherLogin();
}else if(identityid == 3 ){ // 学生登录
StudentFunctionTest.studentLogin();
}else{
System.exit(0);;
}
}
}
}
4、功能展示
这里只展示学生登录完整功能,管理员功能和教师功能页面太多了,这里就不过多展示,需要可以自己测试实现
学生登录
1、查看自己的信息
2、 修改自己的信息
4、查看自己所有科目成绩信息
5、查看自己指定科目成绩信息
6、查看自己班级所有学生所有科目成绩信息
7、查看自己班级所有学生指定科目成绩信息
8、查看自己班级指定学生所有科目成绩信息
9、查看自己班级指定学生指定科目成绩信息
0、退出
3、 删除自己的信息
说明:这里删除自己的信息直接默认退出账号,从student表中直接删除全部信息,再登录这个账号就登录不了了,小心操作。
总结:这个项目非常锻炼个人思维,从数据库表的设计,到代码功能的实现,虽然特别折磨人,但是挺锻炼个人对三层架构,jdbc操作的理解与实现,从这一个项目中体现了面向接口编程,后端全是写接口,实现接口。