石家庄铁道大学选课管理系统
课上的“石家庄铁道大学的选课管理系统”的实验要求;
1、项目需求:
本项目所开发的学生选课系统完成学校对学生的选课信息的统计与管理,减少数据漏掉的情况,同时也节约人力、物力和财力。告别以往的人工统计。
2.系统要求与功能设计
2.1 页面要求
(1)能够在Tomcat服务器中正确部署,并通过浏览器查看;
(2)网站页面整体风格统一
(3)首页(登录页)要求实现不同用户登录后,进入的功能页不相同。
(4)教师功能页:有添加课程、修改个人信息、浏览选课学生信息三个模块。
(5)学生功能页:有修改个人信息、浏览课程信息、选课三个功能模块。
(5)管理员功能页:有添加教师信息、添加学生信息两个模块。
2.2功能要求:
(1)添加教师信息:管理员可以添加教师基本信息,教师基本信息包括:教师工号(八位数字组成,例如02000081)、教师姓名、教师性别、教师所在学院、职称(教授、副教授、讲师、助教)组成;
(2)添加学生信息:管理可以添加学生基本信息,学生基本信息包括学号(八位数字组成,例如20180052)、学生姓名、学生性别、所在班级、所属专业组成;
(3)添加课程信息:教师登陆后,可以添加自己任职的课程基本信息,课程基本信息包括:课程编号(六位数字组成,例如050013),课程名称、选课人数、任课教师(任课教师不需录入,那位教师填写课程信息,那位教师就是任课教师);
(4)修改个人信息:教师或学生登陆后可以修改个人信息,但教师工号或学号不能修改,另外教师或学生只能修改自己的信息,无法看到或修改其他学生或教师的基本信息。
(5)浏览课程信息:学生登陆后可以看到所有课程的列表信息,点击课程名称可以查看课程的详细信息,包括已选课人数;点击教师名称可以查看教师的详细信息。
(6)选课:进入选课页面,课程信息列表显示所有选课人数未达到课程设置的选课人数上限,点击课程名称可以看到课程详细信息,点击课程详细信息页面的“选课”按钮,可以实现选课功能。
(7)浏览选课学生信息:教师进入该页面后,可以看到自己设置的课程信息列表,点击课程名称,可以看到,选择该课程的所有学生基本信息列表。
(8)登陆功能:管理员、教师、学生登陆后可以看到不同的功能页面,教师或学生登陆后只能看到自己的相关信息,不同教师、不同学生登陆后无法查看其他人的信息。(要求至少创建两个教师用户、十个学生用户演示选课过程)
3数据库设计:
要求实现课程基本信息表、教师基本信息表、学生基本信息表、选课基本信息表。(提示:选课基本信息包括课程编号、教师编号、学号等基本信息)
4、WEB发布:
要求可以实现在浏览器直接访问系统。
在课上的时候我只做出来这里面功能的一部分,只达到了等级C,然后课下的时候自己逐渐的完善代码,还是有一部分的功能不能实现,这跟考试的内容相差不多,我等在考试之前完善所有的功能,达到考试要求;
package Bean; public class ChooseClass { String snumber; String cnumber; String sname; String cname; String cteacher; String most; String have; public ChooseClass() { } public ChooseClass(String snumber,String sname,String cnumber,String cname,String cteacher,String most,String have) { this.cname=cname; this.sname=sname; this.snumber=snumber; this.cnumber=cnumber; this.cteacher=cteacher; this.most=most; this.have=have; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSnumber() { return snumber; } public void setSnumber(String snumber) { this.snumber = snumber; } public String getCnumber() { return cnumber; } public void setCnumber(String cnumber) { this.cnumber = cnumber; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public String getCteacher() { return cteacher; } public void setCteacher(String cteacher) { this.cteacher = cteacher; } public String getMost() { return most; } public void setMost(String most) { this.most = most; } public String getHave() { return have; } public void setHave(String have) { this.have = have; } } package Bean; public class Classes { String cnumber; String cname; String cteacher; int most; int have; public Classes(String cnumber,String cname,String cteacher,int most) { this.cnumber=cnumber; this.cname=cname; this.cteacher=cteacher; this.most=most; this.have=0; } public Classes(String cnumber,String cname,String cteacher,int most,int have) { this.cnumber=cnumber; this.cname=cname; this.cteacher=cteacher; this.most=most; this.have=have; } public String getCnumber() { return cnumber; } public void setCnumber(String cnumber) { this.cnumber = cnumber; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public String getCteacher() { return cteacher; } public void setCteacher(String cteacher) { this.cteacher = cteacher; } public int getMost() { return most; } public void setMost(int most) { this.most = most; } public int getHave() { return have; } public void setHave(int have) { this.have = have; } } package Bean; public class Login { String account; String password; public Login(String account,String password) { this.account=account; this.password=password; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } package Bean; public class Student { String sname; String snumber; String ssex; String sgrade; //班级 String specialities; //专业 String password; public Student(String sname,String snumber,String ssex,String sgrade,String specialities) { this.sname=sname; this.snumber=snumber; this.password=snumber; this.ssex=ssex; this.sgrade=sgrade; this.specialities=specialities; } public Student(String sname,String snumber,String password,String ssex,String sgrade,String specialities) { this.sname=sname; this.snumber=snumber; this.password=password; this.ssex=ssex; this.sgrade=sgrade; this.specialities=specialities; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public String getSnumber() { return snumber; } public void setSnumber(String snumber) { this.snumber = snumber; } public String getSsex() { return ssex; } public void setSsex(String ssex) { this.ssex = ssex; } public String getSgrade() { return sgrade; } public void setSgrade(String sgrade) { this.sgrade = sgrade; } public String getSpecialities() { return specialities; } public void setSpecialities(String specialities) { this.specialities = specialities; } } package Bean; public class Teacher { String tname; String tnumber; String tsex; String tacademy; String twork; //职称 String password; public Teacher(String tname,String tnumber,String tsex,String tacademy,String twork) { this.tname=tname; this.tacademy=tacademy; this.tnumber=tnumber; this.tsex=tsex; this.twork=twork; this.password=tnumber; } public String getTname() { return tname; } public void setTname(String tname) { this.tname = tname; } public String getTnumber() { return tnumber; } public void setTnumber(String tnumber) { this.tnumber = tnumber; } public String getTsex() { return tsex; } public void setTsex(String tsex) { this.tsex = tsex; } public String getTacademy() { return tacademy; } public void setTacademy(String tacademy) { this.tacademy = tacademy; } public String getTwork() { return twork; } public void setTwork(String twork) { this.twork = twork; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import Bean.ChooseClass; import Bean.Classes; import DBUtil.DBUtil; public class ChooseDao { public boolean insert(String snumber,String sname, Classes cl) { String sql="insert into choose (snumber,sname,cnumber,cname,cteacher,most,have) values('"+snumber+"','"+sname+"','"+cl.getCnumber()+"','"+cl.getCname()+"','"+cl.getCteacher()+"','"+cl.getMost()+"','"+cl.getHave()+"')"; Connection conn=DBUtil.getConn();//数据库连接,加载驱动 boolean flag=false; if(!id(snumber,cl.getCnumber())) { Statement state=null;//数据库操作 try { state=conn.createStatement();//实例化Statement对象,方便对sql语句进行操作 state.executeUpdate(sql); }catch(Exception e)//当try语句中出现异常时,会执行catch中的语句 { e.printStackTrace();//捕获异常的语句 } finally //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常),经常被用在需要释放资源的情况下。 { DBUtil.close(state, conn); //close释放资源 } ClassesDao cD=new ClassesDao(); cD.chose(cl.getCnumber()); flag=true; } return flag; } public boolean delete(String snumber,String cnumber) { String sql="delete from choose where snumber='"+snumber+"' and cnumber='"+cnumber+"'" ; //删除语句: //delete from table_name where some_colume=some_value Connection conn=DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); }catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public boolean delete(String cnumber) { String sql="delete from choose where cnumber='"+cnumber+"'" ; //删除语句: //delete from table_name where some_colume=some_value Connection conn=DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); }catch (Exception e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public boolean id(String snumber,String cnumber) { boolean flag = false; String sql = "select * from choose where snumber='"+snumber+"' and cnumber='"+cnumber+"' "; Connection conn = DBUtil.getConn(); Statement state = null; ResultSet rs = null; try { state = conn.createStatement(); rs = state.executeQuery(sql); //要用statement类的executeQuery()方法来下达select指令以查询数据库, //executeQuery()方法会把数据库响应的查询结果存放在ResultSet类对象中供我们使用。 while (rs.next()) { flag = true; } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(rs, state, conn); } return flag; } public List<ChooseClass> list(String cnumber){ //插入sql语句 String sql="select * from choose where cnumber = '"+cnumber+"' order by id asc"; System.out.println(sql); List<ChooseClass>list=new ArrayList<>(); //给集合list创造(new)一个存储空间用于存放数据 Connection conn=DBUtil.getConn(); Statement state=null; ResultSet rs=null; try{ state=conn.createStatement(); rs=state.executeQuery(sql); while(rs.next()) {//注意:这里用双引号,ID是表user里的ID列 String snumber=rs.getString("snumber"); String sname=rs.getString("sname"); String cname=rs.getString("cname"); String cteacher=rs.getString("cteacher"); String most=rs.getString("most"); String have=rs.getString("have"); ChooseClass classes=new ChooseClass(snumber,sname, cnumber, cname, cteacher, most, have); list.add(classes); //表示,把bean里的数据存入到list当中 } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return list; } } package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import Bean.Classes; import DBUtil.DBUtil; public class ClassesDao { public boolean insert(Classes classes){ String sql="insert into classes(cnumber,cname,cteacher,most,have) values('"+classes.getCnumber()+"','"+classes.getCname()+"','"+classes.getCteacher()+"','"+classes.getMost()+"','"+classes.getHave()+"')"; boolean flag=false; if(!only(classes.getCnumber())) { Connection conn=DBUtil.getConn();//添加数据库,加载驱动 Statement state=null;//创建statement对象并置空 try { state=conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn);//clase 释放资源 } flag=true; } return flag; } public boolean delete(String cnumber,String cname){ //插入sql 语句 String sql="delete from classes where cnumber='"+cnumber+"' and cname='"+cname+"' "; //删除语句: //delete from table_name where some_colume=some_value Connection conn =DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public boolean chose(String cnumber ){ //插入sql 语句 String sql="update classes set have = have +1 where cnumber = '"+cnumber+"' "; //选课修改语句: Connection conn =DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public boolean unchose(String cnumber ){ //插入sql 语句 String sql="update classes set have = have -1 where cnumber = '"+cnumber+"' "; //选课修改语句: Connection conn =DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public boolean judge(String cnumber){ //插入sql 语句 String sql="select * from classes where cnumber= '"+cnumber+"' "; boolean flag =false; Connection conn=DBUtil.getConn(); Statement state=null; ResultSet rs=null; try{ state=conn.createStatement(); rs=state.executeQuery(sql); if(rs.next()) {//注意:这里用双引号,ID是表user里的ID列 int most=rs.getInt("most"); int have=rs.getInt("have"); if(have<most) { flag=true; } //表示,把bean里的数据存入到list当中 } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return flag; } public boolean update(Classes classes) { //插入sql 语句 String sql="update classes set cname='"+classes.getCname()+"',most='"+classes.getMost()+"' where cnumber='"+classes.getCnumber()+"' "; System.out.println(sql); //update语句: //update table_name set column1=value1,column2=value2 where some_column=some_value; System.out.println(sql); Connection conn=DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public boolean update(String cteacher,String oldname) { //插入sql 语句 String sql="update classes set cteacher='"+cteacher+"' where cteacher='"+oldname+"' "; //update语句: //update table_name set column1=value1,column2=value2 where some_column=some_value; Connection conn=DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public List<Classes> list(String tname){ //插入sql语句 String sql="select * from classes where cteacher = '"+tname+"' order by id asc"; List<Classes>list=new ArrayList<>(); //给集合list创造(new)一个存储空间用于存放数据 Connection conn=DBUtil.getConn(); Statement state=null; ResultSet rs=null; try{ state=conn.createStatement(); rs=state.executeQuery(sql); Classes classes=null; while(rs.next()) {//注意:这里用双引号,ID是表user里的ID列 String cnumber=rs.getString("cnumber"); String cname=rs.getString("cname"); String cteacher=rs.getString("cteacher"); int most=rs.getInt("most"); int have=rs.getInt("have"); classes=new Classes(cnumber,cname,cteacher,most,have); list.add(classes); //表示,把bean里的数据存入到list当中 } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return list; } public List<Classes> list(){ //插入sql语句 String sql="select * from classes order by id asc"; List<Classes>list=new ArrayList<>(); //给集合list创造(new)一个存储空间用于存放数据 Connection conn=DBUtil.getConn(); Statement state=null; ResultSet rs=null; try{ state=conn.createStatement(); rs=state.executeQuery(sql); Classes classes=null; while(rs.next()) {//注意:这里用双引号,ID是表user里的ID列 String cnumber=rs.getString("cnumber"); String cname=rs.getString("cname"); String cteacher=rs.getString("cteacher"); int most=rs.getInt("most"); int have=rs.getInt("have"); classes=new Classes(cnumber,cname,cteacher,most,have); list.add(classes); //表示,把bean里的数据存入到list当中 } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return list; } public boolean only(String cnumber) { //定义标记 boolean flag=false; //插入sql语句 String sql="select * from classes where cnumber = '"+cnumber+"'"; Connection conn=DBUtil.getConn(); Statement state=null; ResultSet rs=null; try { state=conn.createStatement(); rs=state.executeQuery(sql); //要用statement类的executeQuery()方法来下达select指令以查询数据库, //executeQuery()方法会把数据库响应的查询结果存放在ResultSet类对象中供我们使用。 while (rs.next()) { flag = true; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { DBUtil.close(rs, state, conn); } return flag; } } package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import Bean.Login; import DBUtil.DBUtil; public class LoginDao { public boolean insert(Login login){ //插入的sql语句 String sql="insert into login(account,password) values('"+login.getAccount()+"','"+login.getPassword()+"')"; Connection conn=DBUtil.getConn();//添加数据库,加载驱动 Statement state=null;//创建statement对象并置空 try { state=conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public boolean delete(String account){ //插入sql 语句 String sql="delete from login where account='"+account+"' "; String sql1="delete from student where account='"+account+"' "; String sql2="delete from teacher where account='"+account+"' "; //删除语句: //delete from table_name where some_colume=some_value Connection conn =DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); state.executeUpdate(sql1); state.executeUpdate(sql2); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public boolean log(String account,String password){ //插入sql语句 String sql="select * from login where account='"+account+"' and password='"+password+"'"; //看数据库里面是否有数据,提取出来 Connection conn=DBUtil.getConn(); Statement state=null; ResultSet rs=null; boolean flag=false; try{ state=conn.createStatement(); rs=state.executeQuery(sql); if(rs.next()) {//注意:这里用双引号,ID是表user里的ID列 flag=true; //表示,把bean里的数据存入到list当中 } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return flag; } } package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import Bean.Student; import Bean.Teacher; import DBUtil.DBUtil; public class StudentDao { public boolean insert(Student student){ String sql="insert into student(sname,snumber,ssex,sgrade,specialities) values('"+student.getSname()+"','"+student.getSnumber()+"','"+student.getSsex()+"','"+student.getSgrade()+"','"+student.getSpecialities()+"')"; Connection conn=DBUtil.getConn();//添加数据库,加载驱动 Statement state=null;//创建statement对象并置空 try { //实例化statement对象 方便操作 state=conn.createStatement(); state.executeUpdate(sql); //执行数据库更新操作用于执行insert、update或delete语句以及SQLDDL(数据定义语言)语句, //例如creattable和droptable,(创建表和删除表) } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常), //经常被用在需要释放资源的情况下。 finally { DBUtil.close(state, conn);//clase 释放资源 } return false; } public boolean update(Student student) { //插入sql 语句 String sql="update student set sname='"+student.getSname()+"',password='"+student.getPassword()+"',ssex='"+student.getSsex()+"',sgrade='"+student.getSgrade()+"',specialities='"+student.getSpecialities()+"' where snumber='"+student.getSnumber()+"' "; //update语句: //update table_name set column1=value1,column2=value2 where some_column=some_value; Connection conn=DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public Student log(String account,String password){ //插入sql语句 String sql="select * from student where snumber='"+account+"' and password='"+password+"'"; Student student=null; Connection conn=DBUtil.getConn(); Statement state=null; ResultSet rs=null; boolean flag=false; try{ state=conn.createStatement(); rs=state.executeQuery(sql); if(rs.next()) {//注意:这里用双引号,ID是表user里的ID列 String sname=rs.getString("sname"); String snumber=rs.getString("snumber"); String ssex=rs.getString("ssex"); String sgrade=rs.getString("sgrade"); String specialities=rs.getString("specialities"); student=new Student(sname, snumber, ssex, sgrade, specialities); //表示,把bean里的数据存入到list当中 } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return student; } } package Dao; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import Bean.Teacher; import DBUtil.DBUtil; public class TeacherDao { public boolean insert(Teacher teacher){ String sql="insert into teacher(tname,tnumber,tsex,tacademy,twork) values('"+teacher.getTname()+"','"+teacher.getTnumber()+"','"+teacher.getTsex()+"','"+teacher.getTacademy()+"','"+teacher.getTwork()+"')"; Connection conn=DBUtil.getConn();//添加数据库,加载驱动 Statement state=null;//创建statement对象并置空 try { //实例化statement对象 方便操作 state=conn.createStatement(); state.executeUpdate(sql); //执行数据库更新操作用于执行insert、update或delete语句以及SQLDDL(数据定义语言)语句, //例如creattable和droptable,(创建表和删除表) } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } //finally作为异常处理的一部分,它只能用在try/catch语句中,并且附带一个语句块,表示这段语句最终一定会被执行(不管有没有抛出异常), //经常被用在需要释放资源的情况下。 finally { DBUtil.close(state, conn);//clase 释放资源 } return true; } public boolean update(Teacher teacher) { //插入sql 语句 String sql="update teacher set tname='"+teacher.getTname()+"',password='"+teacher.getPassword()+"',tsex='"+teacher.getTsex()+"',tacademy='"+teacher.getTacademy()+"',twork='"+teacher.getTwork()+"' where tnumber='"+teacher.getTnumber()+"' "; //update语句: //update table_name set column1=value1,column2=value2 where some_column=some_value; Connection conn=DBUtil.getConn(); Statement state=null; try { state=conn.createStatement(); state.executeUpdate(sql); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { DBUtil.close(state, conn); } return false; } public Teacher log(String account,String password){ //插入sql语句 String sql="select * from teacher where tnumber='"+account+"' and password='"+password+"'"; Teacher teacher=null; Connection conn=DBUtil.getConn(); Statement state=null; ResultSet rs=null; try{ state=conn.createStatement(); rs=state.executeQuery(sql); if(rs.next()) {//注意:这里用双引号,ID是表user里的ID列 String tname=rs.getString("tname"); String tnumber=rs.getString("tnumber"); String tsex=rs.getString("tsex"); String tacademy=rs.getString("tacademy"); String twork=rs.getString("twork"); teacher=new Teacher(tname, tnumber, tsex, tacademy, twork); //表示,把bean里的数据存入到list当中 } } catch (SQLException e) { e.printStackTrace(); } finally { DBUtil.close(state, conn); } return teacher; } }
package DBUtil; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class DBUtil{ //sql语句字符串 public static String db_url="jdbc:mysql://localhost:3306/chooseclass?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT"; //数据库名 public static String db_name="root"; //数据库密码 public static String db_password="123456"; public static Connection getConn() { //声明connection类型对象,指向空 Connection conn=null; //连接驱动 //链接数据库 try { //驱动程序名 Class.forName("com.mysql.cj.jdbc.Driver"); //具体地连接到数据库——联接字符串(数据库名),联接用户名,联接密码名 conn =DriverManager.getConnection(db_url, db_name, db_password); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } public static void close(Statement state, Connection conn) { if(state !=null) { try { state.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void close(ResultSet rs,Statement state, Connection conn) { if(rs!=null) { try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(state!=null) { try { state.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Bean.Classes; import Bean.Student; import Bean.Teacher; import Dao.ChooseDao; import Dao.ClassesDao; import Dao.StudentDao; import Dao.TeacherDao; /** * Servlet implementation class AddServlet */ @WebServlet("/addServlet") public class AddServlet extends HttpServlet { public AddServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8");//设置从jsp或HTML中请求数据的值,也就是设置为中文,防止乱码 String method=req.getParameter("method");//getParameter()获取的是客户端设置的数据。 if(method.equals("teacher")) { String tname=req.getParameter("name"); String tsex=req.getParameter("sex"); String tnumber=req.getParameter("tnumber"); String tacademy=req.getParameter("tacademy"); String twork=req.getParameter("twork"); Teacher teacher=new Teacher(tname, tnumber, tsex, tacademy, twork); TeacherDao TD=new TeacherDao(); if(TD.insert(teacher)) { //如果添加成功 req.setAttribute("message", "添加成功"); //这里表示,在req的作用域中设置一个massage变量,变量内容是:添加成功 //然后,当跳转到它下一个界面时,这个作用域里的值可以通过Object message =request.getAttribute("message")拿出来使用 req.getRequestDispatcher("admin.jsp").forward(req, resp); } else { //处理失败 req.setAttribute("message", "添加重复,请重新输入"); req.getRequestDispatcher("addTeacher.jsp").forward(req, resp); } } else if(method.equals("student")) { String sname=req.getParameter("sname"); String ssex=req.getParameter("sex"); String snumber=req.getParameter("snumber"); String sgrade=req.getParameter("sgrade"); String specialities=req.getParameter("specialities"); Student student=new Student(sname, snumber, ssex, sgrade, specialities); StudentDao SD=new StudentDao(); if(SD.insert(student)) { //如果添加成功 req.setAttribute("message", "添加成功"); //这里表示,在req的作用域中设置一个massage变量,变量内容是:添加成功 //然后,当跳转到它下一个界面时,这个作用域里的值可以通过Object message =request.getAttribute("message")拿出来使用 req.getRequestDispatcher("admin.jsp").forward(req, resp); } else { //处理失败 req.setAttribute("message", "添加重复,请重新输入"); req.getRequestDispatcher("addStudent.jsp").forward(req, resp); } } else if(method.equals("class")) { String cname=req.getParameter("cname"); String cnumber=req.getParameter("cnumber"); String cteacher=req.getParameter("cteacher"); String most=req.getParameter("most"); int a=Integer.parseInt(most); Classes classes=new Classes(cnumber,cname,cteacher,a); ClassesDao cD = new ClassesDao(); if(cD.insert(classes)) { //如果添加成功 req.setAttribute("message", "添加成功"); //这里表示,在req的作用域中设置一个massage变量,变量内容是:添加成功 //然后,当跳转到它下一个界面时,这个作用域里的值可以通过Object message =request.getAttribute("message")拿出来使用 req.getRequestDispatcher("teacher.jsp").forward(req, resp); } else { //处理失败 req.setAttribute("message", "添加重复,请重新输入"); req.getRequestDispatcher("teacherAddClass.jsp").forward(req, resp); } } else if(method.equals("choose")) { String snumber=req.getParameter("snumber"); String sname=req.getParameter("sname"); String cname=req.getParameter("cname"); String cnumber=req.getParameter("cnumber"); String cteacher=req.getParameter("cteacher"); String most=req.getParameter("most"); String have=req.getParameter("have"); int a=Integer.parseInt(most); int b=Integer.parseInt(have); Classes classes=new Classes(cnumber,cname,cteacher,a); ChooseDao chD=new ChooseDao(); if((a>b)&&chD.insert(snumber,sname,classes)) { //如果添加成功 req.setAttribute("message", "添加成功"); //这里表示,在req的作用域中设置一个massage变量,变量内容是:添加成功 //然后,当跳转到它下一个界面时,这个作用域里的值可以通过Object message =request.getAttribute("message")拿出来使用 req.getRequestDispatcher("selectServlet?method=class").forward(req, resp); } else { //处理失败 req.setAttribute("message", "添加重复或所选课容量已满"); req.getRequestDispatcher("selectServlet?method=class").forward(req, resp); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } package Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Dao.ChooseDao; import Dao.ClassesDao; /** * Servlet implementation class DeleteServlet */ @WebServlet("/deleteServlet") public class DeleteServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method=req.getParameter("method"); if(method.equals("sdclass")) { String cnumber=req.getParameter("cnumber"); String snumber=req.getParameter("snumber"); ChooseDao chD=new ChooseDao(); ClassesDao cD=new ClassesDao(); if(chD.id(snumber, cnumber)) { chD.delete(snumber, cnumber); cD.unchose(cnumber); req.setAttribute("message", "取消选课成功"); req.getRequestDispatcher("selectServlet?method=class").forward(req, resp); } else { req.setAttribute("message", "未选课,取消选课失败"); req.getRequestDispatcher("selectServlet?method=class").forward(req, resp); } } if(method.equals("tdclass")) { String cnumber=req.getParameter("cnumber"); String cname=req.getParameter("cname"); String tname=req.getParameter("tname"); ChooseDao chD=new ChooseDao(); ClassesDao cD=new ClassesDao(); chD.delete( cnumber); cD.delete(cnumber, cname); req.setAttribute("message", "删除课程成功"); req.getRequestDispatcher("updateServlet?method=classlist").forward(req, resp); } if(method.equals("tsclass")) { String cnumber=req.getParameter("cnumber"); String cname=req.getParameter("cname"); String tname=req.getParameter("tname"); ChooseDao chD=new ChooseDao(); ClassesDao cD=new ClassesDao(); chD.delete( cnumber); cD.delete(cnumber, cname); req.setAttribute("message", "删除课程成功"); req.getRequestDispatcher("updateServlet?method=sclasslist").forward(req, resp); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } } package Servlet; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Bean.Student; import Bean.Teacher; import Dao.LoginDao; import Dao.StudentDao; import Dao.TeacherDao; @WebServlet("/loginServlet") public class LoginServlet extends HttpServlet { public LoginServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { LoginDao login=new LoginDao(); TeacherDao tD=new TeacherDao(); StudentDao sD=new StudentDao(); String number=request.getParameter("account"); String password=request.getParameter("password"); String account=request.getParameter("account"); if(number.length()==6) { if(login.log(account, password)) { Teacher teacher=null; teacher=tD.log(account, password); request.getSession().setAttribute("teacher", teacher); request.setAttribute("message", "恭喜"+teacher.getTname()+"登陆成功"); request.getRequestDispatcher("teacher.jsp").forward(request, response); } else { request.setAttribute("message", "账号或密码错误"); request.getRequestDispatcher("index.jsp").forward(request, response); } }else if(number.length()==8) { if(login.log(account, password)) { Student student=null; student=sD.log(account, password); request.getSession().setAttribute("student", student); request.setAttribute("message", "恭喜"+student.getSname()+"登陆成功"); request.getRequestDispatcher("student.jsp").forward(request, response); } else { request.setAttribute("message", "账号或密码错误"); request.getRequestDispatcher("index.jsp").forward(request, response); } } else if(number.equals("admin")) { if(password.equals("123456")) { request.setAttribute("message", "登陆成功"); request.getRequestDispatcher("admin.jsp").forward(request, response); } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } package Servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Bean.ChooseClass; import Bean.Classes; import Dao.ChooseDao; import Dao.ClassesDao; /** * Servlet implementation class SelectServlet */ @WebServlet("/selectServlet") public class SelectServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { ClassesDao cD =new ClassesDao(); ChooseDao chD=new ChooseDao(); String method=req.getParameter("method"); if(method.equals("class")) { List<Classes> class_list =cD.list(); req.setAttribute("class_list", class_list); req.getRequestDispatcher("studentChoose.jsp").forward(req, resp); } else if(method.equals("teacherSC")) { String tname=req.getParameter("tname"); List<Classes> class_list =cD.list(tname); req.setAttribute("class_list", class_list); req.getRequestDispatcher("teacherSelectClass.jsp").forward(req, resp); } else if(method.equals("SSC")) { String cnumber=req.getParameter("cnumber"); System.out.println(cnumber); List<ChooseClass> class_list =chD.list(cnumber); req.setAttribute("class_list", class_list); req.getRequestDispatcher("teacherChooseClass.jsp").forward(req, resp); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } } package Servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import Bean.Classes; import Bean.Student; import Bean.Teacher; import Dao.ChooseDao; import Dao.ClassesDao; import Dao.StudentDao; import Dao.TeacherDao; /** * Servlet implementation class UpdateServlet */ @WebServlet("/updateServlet") public class UpdateServlet extends HttpServlet { private static final long serialVersionUID = 1L; public UpdateServlet() { super(); // TODO Auto-generated constructor stub } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method=req.getParameter("method"); if(method.equals("teacher")) { String tname=req.getParameter("tname"); String tsex=req.getParameter("tsex"); String tnumber=req.getParameter("tnumber"); String tacademy=req.getParameter("tacademy"); String twork=req.getParameter("twork"); String oldname=req.getParameter("oldname"); Teacher teacher=new Teacher(tname, tnumber, tsex, tacademy, twork); TeacherDao TD=new TeacherDao(); ClassesDao cD=new ClassesDao(); TD.update(teacher); cD.update(tname, oldname); //对数据库进行修改; req.setAttribute("message", "修改成功"); req.getSession().setAttribute("teacher", teacher); req.getRequestDispatcher("teacher.jsp").forward(req,resp); } else if(method.equals("student")) { String sname=req.getParameter("sname"); String snumber=req.getParameter("snumber"); String password=req.getParameter("password"); String ssex=req.getParameter("ssex"); String sgrade=req.getParameter("sgrade"); String specialities=req.getParameter("specialities"); Student student=new Student(sname, snumber, password,ssex,sgrade, specialities); StudentDao sD = new StudentDao(); sD.update(student); //对数据库进行修改; req.setAttribute("message", "修改成功"); req.getSession().setAttribute("student", student); req.getRequestDispatcher("student.jsp").forward(req,resp); } else if(method.equals("classlist")) { ClassesDao cD = new ClassesDao(); //List<Classes> class_list = cD.list((String) req.getSession().getAttribute("teacher.tname")); String tn=req.getParameter("tname"); List<Classes> class_list = cD.list(tn); req.getSession().setAttribute("class_list", class_list); req.getRequestDispatcher("classUpdate.jsp").forward(req,resp); } else if(method.equals("sclasslist")) { ClassesDao cD = new ClassesDao(); //List<Classes> class_list = cD.list((String) req.getSession().getAttribute("teacher.tname")); String tn=req.getParameter("tname"); List<Classes> class_list = cD.list(tn); req.getSession().setAttribute("class_list", class_list); req.getRequestDispatcher("teacherSelectClass.jsp").forward(req,resp); } else if(method.equals("class")) { String cname=req.getParameter("cname"); String cnumber=req.getParameter("cnumber"); String cteacher=req.getParameter("cteacher"); String most=req.getParameter("most"); int a=Integer.parseInt(most); Classes cl =new Classes(cnumber, cname,cteacher,a); ClassesDao cD = new ClassesDao(); cD.update(cl); //对数据库进行修改; req.setAttribute("message", "修改成功"); req.getSession().setAttribute("cl", cl); req.getRequestDispatcher("teacher.jsp").forward(req,resp); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
还有各种界面
管理员的登录
教师的登录