学生选课系统
题目要求
实现添加教师信息,添加学生信息,添加课程信息,修改个人信息,浏览课程信息,选课,浏览学生选课信息,登录这几个功能
此程序使用的jar包如下,如想应用此程序,请先下载相应jar包
首先是bean层
我在数据库中创建了五个表用来存储数据
首先是管理员,代码如下:
package bean; public class Guanliyuan { String id; //管理员登录账号 String password; //管理员登录密码 @Override public String toString() { return "Guanliyuan{" + "id='" + id + '\'' + ", password='" + password + '\'' + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Guanliyuan(String id, String password) { this.id = id; this.password = password; } public Guanliyuan() { } }
第二个是课程表,用于存储选课学生的信息
package bean; public class Kecheng { String id; //选课学生的学号 String name; //选课学生的姓名 String sex; //选课学生的性别 String banji; //选课学生的班级 String zhuanye; //选课学生的专业 String classname; //选课学生的课程名称 @Override public String toString() { return "Kecheng{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", banji='" + banji + '\'' + ", zhuanye='" + zhuanye + '\'' + ", classname='" + classname + '\'' + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBanji() { return banji; } public void setBanji(String banji) { this.banji = banji; } public String getZhuanye() { return zhuanye; } public void setZhuanye(String zhuanye) { this.zhuanye = zhuanye; } public String getClassname() { return classname; } public void setClassname(String classname) { this.classname = classname; } public Kecheng(String id, String name, String sex, String banji, String zhuanye, String classname) { this.id = id; this.name = name; this.sex = sex; this.banji = banji; this.zhuanye = zhuanye; this.classname = classname; } public Kecheng() { } }
第三个表为lesson表,用于存储教师添加的课程信息
package bean; public class Lesson { String id; 课程编码 String name; 课程名称 String nummber; 课程最大选课人数 String teacher; 课程任课教师 @Override public String toString() { return "Lesson{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", nummber='" + nummber + '\'' + ", teacher='" + teacher + '\'' + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getNummber() { return nummber; } public void setNummber(String nummber) { this.nummber = nummber; } public String getTeacher() { return teacher; } public void setTeacher(String teacher) { this.teacher = teacher; } public Lesson(String id, String name, String nummber, String teacher) { this.id = id; this.name = name; this.nummber = nummber; this.teacher = teacher; } public Lesson() { } }
第四个表为student表,用于储存学生信息
package bean; public class Student { String id; 学生学号 String name; 学生姓名 String sex; 学生性别 String banji; 学生班级 String zhuanye; 学生专业 String password; 学生密码 @Override public String toString() { return "Student{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", banji='" + banji + '\'' + ", zhuanye='" + zhuanye + '\'' + ", password='" + password + '\'' + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBanji() { return banji; } public void setBanji(String banji) { this.banji = banji; } public String getZhuanye() { return zhuanye; } public void setZhuanye(String zhuanye) { this.zhuanye = zhuanye; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Student(String id, String name, String sex, String banji, String zhuanye, String password) { this.id = id; this.name = name; this.sex = sex; this.banji = banji; this.zhuanye = zhuanye; this.password = password; } public Student() { } }
第五个表为teacher表,用于储存教师信息
package bean; public class Teacher { String id; 教师工号 String name; 教师姓名 String sex; 教师性别 String xueyuan; 教师学院 String zhicheng; 教师职称 String password; 教师密码 @Override public String toString() { return "Teacher{" + "id='" + id + '\'' + ", name='" + name + '\'' + ", sex='" + sex + '\'' + ", xueyuan='" + xueyuan + '\'' + ", zhicheng='" + zhicheng + '\'' + ", password='" + password + '\'' + '}'; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getXueyuan() { return xueyuan; } public void setXueyuan(String xueyuan) { this.xueyuan = xueyuan; } public String getZhicheng() { return zhicheng; } public void setZhicheng(String zhicheng) { this.zhicheng = zhicheng; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Teacher(String id, String name, String sex, String xueyuan, String zhicheng, String password) { this.id = id; this.name = name; this.sex = sex; this.xueyuan = xueyuan; this.zhicheng = zhicheng; this.password = password; } public Teacher() { } }
dao层用于对各个表的操作
guanliyuandao因为只需要实现登录功能,所以guanliyuan表只需要实现一个查找功能便可
package dao.guanliyuan; import bean.Guanliyuan; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import util.DButil; import java.sql.SQLException; public class Guanliyuandao { private QueryRunner queryRunner =new QueryRunner(DButil.getDruidDataSource()); public Guanliyuan select(String id) { Guanliyuan guanliyuan = null; try { guanliyuan = queryRunner.query("select * from guanliyuan where id=?",new BeanHandler<Guanliyuan>(Guanliyuan.class),id); } catch (SQLException e) { e.printStackTrace(); } return guanliyuan; } }
kechengdao因为需要实现选课功能与查看选课学生信息的功能,所以需要查找与插入两个功能
package dao.kecheng; import bean.Kecheng; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import util.DButil; import java.sql.SQLException; import java.util.List; public class Kechengdao { private QueryRunner queryRunner =new QueryRunner(DButil.getDruidDataSource()); public boolean insert(Kecheng kecheng) { Object[] parms={kecheng.getId(),kecheng.getName(),kecheng.getSex(),kecheng.getBanji(),kecheng.getZhuanye(),kecheng.getClassname()}; try { int result = queryRunner.update("insert into kecheng(id,name,sex,banji,zhuanye,classname) value (?,?,?,?,?,?)",parms); if(result == 1) return true; } catch (SQLException e) { e.printStackTrace(); } return false; } public List<Kecheng> select(String classname) { List<Kecheng> kechengList = null; try { kechengList = queryRunner.query("select * from kecheng where classname=?",new BeanListHandler<Kecheng>(Kecheng.class),classname); return kechengList; } catch (SQLException e) { e.printStackTrace(); } return kechengList; } }
lessondao因为要实现添加课程与查看可选课程,所以要实现插入,根据教师姓名查找,根据课程名称查找
package dao.lesson; import bean.Lesson; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import util.DButil; import java.sql.SQLException; import java.util.List; public class Lessondao { private QueryRunner queryRunner =new QueryRunner(DButil.getDruidDataSource()); public boolean insert(Lesson lesson) { Object[] parms={lesson.getId(),lesson.getName(),lesson.getNummber(),lesson.getTeacher()}; try { int result =queryRunner.update("insert into lesson(id,name,nummber,teacher) value (?,?,?,?)",parms); if(result == 1) return true; } catch (SQLException e) { e.printStackTrace(); } return false; } public boolean update(Lesson lesson) { Object[] parms={lesson.getName(),lesson.getNummber(),lesson.getId()}; try { int result = queryRunner.update("update student set name=?,nummber=? where id=?",parms); if(result == 1) return true; } catch (SQLException e) { e.printStackTrace(); } return false; } public Lesson select(String id) { Lesson lesson = null; try { lesson = queryRunner.query("select * from lesson where id=?",new BeanHandler<Lesson>(Lesson.class),id); } catch (SQLException e) { e.printStackTrace(); } return lesson; }public Lesson selectname(String name) { Lesson lesson = null; try { lesson = queryRunner.query("select * from lesson where name=?",new BeanHandler<Lesson>(Lesson.class),name); } catch (SQLException e) { e.printStackTrace(); } return lesson; }public List<Lesson> selectteacher(String teacher) { List<Lesson> lesson = null; try { lesson = queryRunner.query("select * from lesson where teacher=?",new BeanListHandler<Lesson>(Lesson.class),teacher); } catch (SQLException e) { e.printStackTrace(); } return lesson; } public List<Lesson> selectAll() { try { List<Lesson> lessonList = queryRunner.query("select * from lesson",new BeanListHandler<Lesson>(Lesson.class)); return lessonList; } catch (SQLException e) { e.printStackTrace(); } return null; } }
studentdao需要插入,查找,修改的功能
package dao.student; import bean.Student; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import util.DButil; import java.sql.SQLException; public class Studentdao { private QueryRunner queryRunner =new QueryRunner(DButil.getDruidDataSource()); public boolean insert(Student student) { Object[] parms={student.getId(),student.getName(),student.getSex(),student.getBanji(),student.getZhuanye(),student.getPassword()}; try { int result =queryRunner.update("insert into student(id,name,sex,banji,zhuanye,password) value (?,?,?,?,?,?)",parms); if(result == 1) return true; } catch (SQLException e) { e.printStackTrace(); } return false; } public boolean update(Student student) { Object[] parms={student.getName(),student.getSex(),student.getBanji(),student.getZhuanye(),student.getPassword(),student.getId(),}; try { int result = queryRunner.update("update student set name=?,sex=?,banji=?,zhuanye=?,password=? where id=?",parms); if(result == 1) return true; } catch (SQLException e) { e.printStackTrace(); } return false; } public Student select(String id) { Student student = null; try { student = queryRunner.query("select * from student where id=?",new BeanHandler<Student>(Student.class),id); } catch (SQLException e) { e.printStackTrace(); } return student; } }
teacherdao同样需要实现插入,修改,查找的功能
package dao.teacher; import bean.Teacher; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import util.DButil; import java.sql.SQLException; public class Teacherdao { private QueryRunner queryRunner =new QueryRunner(DButil.getDruidDataSource()); public boolean insert(Teacher teacher) { Object[] parms={teacher.getId(),teacher.getName(),teacher.getSex(),teacher.getXueyuan(),teacher.getZhicheng(),teacher.getPassword()}; try { int result =queryRunner.update("insert into teacher(id,name,sex,xueyuan,zhicheng,password) value (?,?,?,?,?,?)",parms); if(result == 1) return true; } catch (SQLException e) { e.printStackTrace(); } return false; } public boolean update(Teacher teacher) { Object[] parms = {teacher.getName(),teacher.getSex(),teacher.getXueyuan(),teacher.getZhicheng(),teacher.getPassword(),teacher.getId()}; try { int result = queryRunner.update("update teacher set name=?,sex=?,xueyuan=?,zhicheng=?,password=? where id=?",parms); if(result == 1) return true; } catch (SQLException e) { e.printStackTrace(); } return false; } public Teacher select(String id) { Teacher teacher = null; try { teacher = queryRunner.query("select * from teacher where id=?",new BeanHandler<Teacher>(Teacher.class),id); } catch (SQLException e) { e.printStackTrace(); } return teacher; }public Teacher selectname(String name) { Teacher teacher = null; try { teacher = queryRunner.query("select * from teacher where name=?",new BeanHandler<Teacher>(Teacher.class),name); } catch (SQLException e) { e.printStackTrace(); } return teacher; } }
util层
package util; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; import java.io.IOException; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class DButil { private static DruidDataSource druidDataSource; static { Properties properties = new Properties(); InputStream inputStream = DButil.class.getResourceAsStream("/datebase.properties"); try { properties.load(inputStream); try { druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } catch (IOException e) { e.printStackTrace(); } } public static Connection getconnection() { try { return druidDataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } public static DruidDataSource getDruidDataSource() { return druidDataSource; } public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) { try { if(resultSet != null) resultSet.close(); if(statement != null) statement.close(); if(connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }
datebase.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/shujuku?&useUnicode=true&serverTimezone=UTC username=root password=123456 #?????? initialSize=10 #?????? maxActive=50 #?????? maxIdle=5 #?????? maxWait=3000
servlet层
addlesson
package servlet; import bean.Lesson; import dao.lesson.Lessondao; 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 java.io.IOException; import java.io.PrintWriter; @WebServlet("/addlesson") public class addlesson extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/http;charset=utf-8"); String id; String name; String nummber; String teacher; teacher = req.getParameter("teacher"); // System.out.println(teacher); id = req.getParameter("id"); name = req.getParameter("name"); nummber = req.getParameter("nummber"); Lesson lesson = new Lesson(id,name,nummber,teacher); Lessondao lessondao = new Lessondao(); PrintWriter printWriter = resp.getWriter(); // lessondao.insert(lesson); if(lessondao.insert(lesson)) printWriter.println("添加成功"); else printWriter.println("添加失败"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
addstudent
package servlet; import bean.Student; import dao.student.Studentdao; 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 java.io.IOException; import java.io.PrintWriter; @WebServlet("/addstudent") public class addstudent extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/http;charset=utf-8"); String id; String name; String sex; String banji; String zhuanye; String password; id = req.getParameter("id"); name = req.getParameter("name"); sex = req.getParameter("sex"); banji = req.getParameter("banji"); zhuanye = req.getParameter("zhuanye"); password = req.getParameter("password"); Student student= new Student(id,name,sex,banji,zhuanye,password); // System.out.println(student); Studentdao studentdao = new Studentdao(); // studentdao.insert(student); PrintWriter printWriter = resp.getWriter(); if(studentdao.insert(student)) printWriter.println("添加成功"); else printWriter.println("添加失败"); // if(studentdao.insert(student)) // System.out.println("插入成功"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
addteacher
package servlet; import bean.Teacher; import dao.teacher.Teacherdao; 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 java.io.IOException; import java.io.PrintWriter; @WebServlet("/addteacher") public class addteacher extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/http;charset=utf-8"); // System.out.println("跳转成功" ); String id; String name; String sex; String xueyuan; String zhicheng; String password; id = req.getParameter("id"); name = req.getParameter("name"); sex = req.getParameter("sex"); xueyuan = req.getParameter("xueyuan"); zhicheng = req.getParameter("zhicheng"); password = req.getParameter("password"); Teacher teacher = new Teacher(id,name,sex,xueyuan,zhicheng,password); Teacherdao teacherdao = new Teacherdao(); PrintWriter printWriter = resp.getWriter(); if(teacherdao.insert(teacher)) printWriter.println("添加成功"); else printWriter.println("添加失败"); // System.out.println("插入成功"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
login
package servlet; import bean.Guanliyuan; import bean.Student; import bean.Teacher; import dao.guanliyuan.Guanliyuandao; import dao.student.Studentdao; import dao.teacher.Teacherdao; 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 java.io.IOException; import java.io.PrintWriter; @WebServlet("/login") public class login extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // req.getRequestDispatcher("/add.html").forward(req,resp); req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); String id; String password; String shenfen; String name = null; id = req.getParameter("id"); password = req.getParameter("password"); shenfen = req.getParameter("shenfen"); PrintWriter printWriter = resp.getWriter(); if("student".equals(shenfen)) { Student student = null; Studentdao studentdao = new Studentdao(); student = studentdao.select(id); if(student != null) { name = student.getName(); if(student.getPassword().equals(password)) { req.setAttribute("id",id); req.setAttribute("password",password); req.setAttribute("name",name); req.getRequestDispatcher("/student.jsp").forward(req,resp); } else { printWriter.println("密码错误"); } } else { printWriter.println("账号不存在"); } }if("teacher".equals(shenfen)) { Teacher teacher = null; Teacherdao teacherdao = new Teacherdao(); teacher = teacherdao.select(id); if(teacher != null) { name = teacher.getName(); if(teacher.getPassword().equals(password)) { req.setAttribute("id",id); req.setAttribute("password",password); req.setAttribute("name",name); req.getRequestDispatcher("/teacher.jsp").forward(req,resp); } else { printWriter.println("密码错误"); } } else { printWriter.println("账号不存在"); } }if("guanliyuan".equals(shenfen)) { Guanliyuan guanliyuan = null; Guanliyuandao guanliyuandao = new Guanliyuandao(); guanliyuan = guanliyuandao.select(id); if(guanliyuan != null) { if(guanliyuan.getPassword().equals(password)) { req.setAttribute("id",id); req.setAttribute("password",password); req.getRequestDispatcher("/guanliyuan.jsp").forward(req,resp); } else { printWriter.println("密码错误"); } } else { printWriter.println("账号不存在"); } } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
select
package servlet; import bean.Kecheng; import bean.Student; import dao.kecheng.Kechengdao; import dao.student.Studentdao; 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 java.io.IOException; import java.io.PrintWriter; @WebServlet("/select") public class select extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); String id = req.getParameter("id"); String classname = req.getParameter("classname"); Student student = null; Studentdao studentdao = new Studentdao(); student = studentdao.select(id); String name = student.getName(); String sex = student.getSex(); String banji = student.getBanji(); String zhuanye = student.getZhuanye(); Kecheng kecheng = new Kecheng(id,name,sex,banji,zhuanye,classname); Kechengdao kechengdao = new Kechengdao(); // kechengdao.insert(kecheng); PrintWriter printWriter = resp.getWriter(); if(kechengdao.insert(kecheng)) printWriter.println("选课成功"); else printWriter.println("选课失败"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
selectlesson
package servlet; import bean.Kecheng; import dao.kecheng.Kechengdao; 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 java.io.IOException; import java.io.PrintWriter; import java.util.List; @WebServlet("/selectlesson") public class selectlesson extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); String classname = req.getParameter("classname"); String id = null; id = req.getParameter("id"); List<Kecheng> kechengList = null; Kecheng kecheng = null; Kechengdao kechengdao = new Kechengdao(); kechengList = kechengdao.select(classname); PrintWriter printWriter = resp.getWriter(); if(id == null) { printWriter.println("<table align='center' border='1'>"); for(int i = 0;i<kechengList.size();i++) { kecheng = kechengList.get(i); printWriter.println("<tr>"); printWriter.println("<td>"); printWriter.println("学生学号:"+ kecheng.getId()); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println("学生姓名:"+kecheng.getName()); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println("学生性别:"+kecheng.getSex()); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println("学生班级:"+kecheng.getBanji()); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println("学生专业:"+kecheng.getZhuanye()); printWriter.println("</td>"); // printWriter.println("<br/>"); printWriter.println("</tr>"); } printWriter.println("</table>"); } else { req.setAttribute("id",id); req.setAttribute("kechengList",kechengList); req.setAttribute("classname",classname); req.getRequestDispatcher("/select.jsp").forward(req,resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
selectteacher
package servlet; import bean.Teacher; import dao.teacher.Teacherdao; 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 java.io.IOException; import java.io.PrintWriter; @WebServlet("/selectteacher") public class selectteacher extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html;charset=utf-8"); PrintWriter printWriter = resp.getWriter(); String name; name = req.getParameter("name"); System.out.println(name); Teacher teacher = null; Teacherdao teacherdao = new Teacherdao(); teacher = teacherdao.selectname(name); printWriter.println("<table align='center' border='1'>"); printWriter.println("<tr>"); printWriter.println("<td>"); printWriter.println("教师工号:"+teacher.getId()+" "); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println("教师姓名:"+teacher.getName()+" "); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println("教师性别:"+teacher.getSex()+" "); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println("教师学院:"+teacher.getXueyuan()+" "); printWriter.println("</td>"); printWriter.println("<td>"); printWriter.println("教师职称:"+teacher.getZhicheng()+" "); printWriter.println("</td>"); printWriter.println("</tr>"); printWriter.println("</table>"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
showclass
package servlet; import bean.Lesson; import dao.lesson.Lessondao; 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 java.io.IOException; import java.util.List; @WebServlet("/showclass") public class showclass extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String id = req.getParameter("id"); List<Lesson> lessonList = null; Lessondao lessondao = new Lessondao(); lessonList = lessondao.selectAll(); for(int i = 0;i<lessonList.size();i++) { System.out.println(lessonList.get(i)); } req.setAttribute("id",id); req.setAttribute("lessonList",lessonList); req.getRequestDispatcher("/selectclass.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
showlesson
package servlet; import bean.Lesson; import dao.lesson.Lessondao; 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 java.io.IOException; import java.util.List; @WebServlet("/showLesson") public class showLesson extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { List<Lesson> lessonList = null; Lessondao lessondao = new Lessondao(); lessonList = lessondao.selectAll(); // for(int i = 0;i<lessonList.size();i++) // { // System.out.println(lessonList.get(i)); // } req.setAttribute("lessonList",lessonList); req.getRequestDispatcher("/selectlesson.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
showteacherlesson
package servlet; import bean.Lesson; import dao.lesson.Lessondao; 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 java.io.IOException; import java.util.List; @WebServlet("/showteacherlesson") public class showteacherlesson extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String name = req.getParameter("name"); List<Lesson> lessonList = null; Lessondao lessondao = new Lessondao(); lessonList = lessondao.selectteacher(name); req.setAttribute("lessonList",lessonList); req.getRequestDispatcher("/showteacherlesson.jsp").forward(req,resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
updatestudent
package servlet; import bean.Student; import dao.student.Studentdao; 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 java.io.IOException; import java.io.PrintWriter; @WebServlet("/updatestudent") public class updatestudent extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String id; String name; String sex; String banji; String zhuanye; String password; id = req.getParameter("id"); name = req.getParameter("name"); sex = req.getParameter("sex"); banji = req.getParameter("banji"); zhuanye = req.getParameter("zhuanye"); password = req.getParameter("password"); Student student = new Student(id, name,sex,banji,zhuanye,password); Studentdao studentdao = new Studentdao(); // studentdao.update(student); resp.setContentType("text/html;charset=utf-8"); PrintWriter printWriter = resp.getWriter(); if(studentdao.update(student)) printWriter.println("修改成功"); else printWriter.println("修改失败"); // if(studentdao.update(student)) // System.out.println("yes"); // else // System.out.println("no"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
updateteacher
package servlet; import bean.Teacher; import dao.teacher.Teacherdao; 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 java.io.IOException; import java.io.PrintWriter; @WebServlet("/updateteacher") public class updateteacher extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String id; String name; String sex; String xueyuan; String zhicheng; String password; id = req.getParameter("id"); name = req.getParameter("name"); sex = req.getParameter("sex"); xueyuan = req.getParameter("xueyuan"); zhicheng = req.getParameter("zhicheng"); password = req.getParameter("password"); Teacher teacher= new Teacher(id,name,sex,xueyuan,zhicheng,password); // System.out.println(teacher); Teacherdao teacherdao = new Teacherdao(); // teacherdao.update(teacher); resp.setContentType("text/html;charset=utf-8"); PrintWriter printWriter = resp.getWriter(); if(teacherdao.update(teacher)) printWriter.println("修改成功"); else printWriter.println("修改失败"); // if(teacherdao.update(teacher)) // System.out.println("修改成功"); // else // System.out.println("修改失败"); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req,resp); } }
web层
add.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>管理员界面</title> </head> <body> <h1>管理员界面</h1> <table border="1" align="center"> <tr> <td> <a href="addteacher.html">添加教师信息 </a> </td> </tr><tr> <td> <a href="addstudent.html">添加学生信息 </a> </td> </tr> </table> </body> </html>
addlesson.jap
<%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/27 Time: 19:01 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%String name = request.getParameter("name");%> <html> <head> <title>添加课程信息</title> </head> <body> <%--<%=name%>--%> <form action="/select_war_exploded/addlesson" method="get"> <table border="2" align="center"> <tr> <td>课程编号</td> <td> <input type="text" name="id" value="请输入六位数字"> </td> </tr> <tr> <td>课程名称</td> <td> <input type="text" name="name"> </td> </tr> <tr> <td>选课人数</td> <td> <input type="text" name="nummber"> </td> </tr><tr> <td> <input type="hidden" name="teacher" value="<%=name%>"> <input type="submit" value="添加"> </td> </tr> </table> </form> </body> </html>
addstudent.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加学生信息</title> </head> <body> <form action="/select_war_exploded/addstudent" method="get"> <table border="2" align="center"> <tr> <td>学号</td> <td> <input type="text" name="id" value="请输入八位数字"> </td> </tr> <tr> <td>姓名</td> <td> <input type="text" name="name"> </td> </tr> <tr> <td>性别</td> <td> <select name="sex"> <option value="null">请选择性别</option> <option value="男">男</option> <option value="女">女</option> </select> </td> </tr> <tr> <td>班级</td> <td> <input type="text" name="banji"> </td> </tr> <tr> <td>专业</td> <td> <input type="text" name="zhuanye"> </td> </tr> <tr> <td>密码</td> <td> <input type="text" name="password"> </td> </tr> <tr> <td> <input type="submit" value="添加"> </td> </tr> </table> </form> </body> </html>
addteacher.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>添加教师信息</title> </head> <body> <form action="/select_war_exploded/addteacher" method="get"> <table border="2" align="center"> <tr> <td>工号</td> <td> <input type="text" name="id" value="请输入8位数字"> </td> </tr> <tr> <td>姓名</td> <td> <input type="text" name="name"> </td> </tr> <tr> <td>性别</td> <td> <select name="sex"> <option value="null">请选择性别</option> <option value="男">男</option> <option value="女">女</option> </select> </td> </tr> <tr> <td>学院</td> <td> <input type="text" name="xueyuan"> </td> </tr> <tr> <td>职称</td> <td> <select name="zhicheng"> <option value="null">请选择职称</option> <option value="教授">教授</option> <option value="副教授">副教授</option> <option value="讲师">讲师</option> <option value="助教">助教</option> </select> </td> </tr> <tr> <td>密码</td> <td> <input type="text" name="password"> </td> </tr> <tr> <td> <input type="submit" value="添加"> </td> </tr> </table> </form> </body> </html>
guanliyuan.jsp
<%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/27 Time: 18:53 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>管理员界面</title> </head> <body> <table border="1" align="center"> <tr> <td> <a href="addteacher.html">添加教师信息 </a> </td> </tr><tr> <td> <a href="addstudent.html">添加学生信息 </a> </td> </tr> </table> </body> </html>
login.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <form action="/select_war_exploded/login" method="get"> <table border="2" align="center"> <tr> <td>账号(学生为学号,教师为工号)</td> <td> <input type="text" name="id"> </td> </tr> <tr> <td>密码</td> <td> <input type="password" name="password"> </td> </tr> <tr> <td>身份</td> <td> <select name="shenfen"> <option value="null">请选择身份</option> <option value="student">学生</option> <option value="teacher">教师</option> <option value="guanliyuan">管理员</option> </select> </td> </tr> <tr> <td> <input type="submit" value="登录"> </td> </tr> </table> </form> </body> </html>
select.jsp
<%@ page import="java.util.List" %> <%@ page import="bean.Kecheng" %> <%@ page import="bean.Lesson" %><%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/28 Time: 0:03 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String id = (String) request.getAttribute("id");%> <% String classname = (String) request.getAttribute("classname");%> <% List<Kecheng> kechengList = (List<Kecheng>) request.getAttribute("kechengList");%> <html> <head> <title>选课详情</title> </head> <body> <%--<%=id%>--%> <%--<%=classname%>--%> <form action="/select_war_exploded/select" method="get"> <table border="1" align="center"> <% for(int i = 0;i < kechengList.size();i++) { Kecheng kecheng = kechengList.get(i); %> <tr> <td>学生学号:<%=kecheng.getId()%> </td> <td>学生姓名:<%=kecheng.getName()%> </td> <td>学生性别:<%=kecheng.getSex()%></td> <td>学生班级:<%=kecheng.getBanji()%> </td> <td>学生专业:<%=kecheng.getZhuanye()%></td> </tr> <% } %><tr> <input type="hidden" name="id" value="<%=id%>"> <input type="hidden" name="classname" value="<%=classname%>"> <td><input type="submit" value="选课"></td> </tr> </table> </form> </body> </html>
selectclass.jsp
<%@ page import="java.util.List" %> <%@ page import="bean.Lesson" %><%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/27 Time: 23:48 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String id = (String) request.getAttribute("id");%> <% List<Lesson> lessonList = (List<Lesson>) request.getAttribute("lessonList");%> <html> <head> <title>选课</title> </head> <body> <table align="center" border="1"> <% for(int i = 0;i < lessonList.size();i++) { Lesson lesson = lessonList.get(i); %> <tr> <td>课程编号:<%=lesson.getId()%></td> <td>课程名称:<a href="/select_war_exploded/selectlesson?classname=<%=lesson.getName()%>&id=<%=id%>"> <%=lesson.getName()%> </a></td> <td>课程最大人数:<%=lesson.getNummber()%></td> <td>任课老师:<%=lesson.getTeacher()%> </td> </tr> <% } %> </table> </body> </html>
selectlesson.jsp
<%@ page import="bean.Lesson" %> <%@ page import="java.util.List" %><%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/27 Time: 20:24 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% List<Lesson> lessonList = (List<Lesson>)request.getAttribute("lessonList");%> <html> <head> <title>选课系统</title> </head> <body> <table border="1" align="center"> <% for(int i = 0;i < lessonList.size();i++) { Lesson lesson = lessonList.get(i); %> <tr> <td>课程编号:<%=lesson.getId()%></td> <td>课程名称:<a href="/select_war_exploded/selectlesson?classname=<%=lesson.getName()%>"> <%=lesson.getName()%> </a></td> <td>课程最大人数:<%=lesson.getNummber()%> </td> <td>任课老师:<a href="/select_war_exploded/selectteacher?name=<%=lesson.getTeacher()%>"> <%=lesson.getTeacher()%> </a></td> </tr> <% } %> </table> </body> </html>
showteacherlesson.jsp
<%@ page import="java.util.List" %> <%@ page import="bean.Lesson" %><%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/28 Time: 11:03 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%List<Lesson> lessonList = (List<Lesson>) request.getAttribute("lessonList");%> <html> <head> <title>课程</title> </head> <body> <table align="center" border="1"> <% for(int i = 0;i < lessonList.size();i++) { Lesson lesson = lessonList.get(i); %> <tr> <td>课程编号:<%=lesson.getId()%> </td> <td>课程名称:<a href="/select_war_exploded/selectlesson?classname=<%=lesson.getName()%>"> <%=lesson.getName()%> </a> </td> <td>课程最大人数:<%=lesson.getNummber()%> </td> <td>任课老师:<%=lesson.getTeacher()%></td> </tr> <% } %> </table> </body> </html>
student.jsp
<%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/27 Time: 19:54 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String id = (String)request.getAttribute("id"); %> <% String name = (String)request.getAttribute("name"); %> <html> <head> <title>学生页面</title> </head> <body> <table border="1" align="center"> 欢迎您<%=name%>同学 <tr> <td> <a href="/select_war_exploded/showclass?id=<%=id%>" >选课 </a> </td> </tr><tr> <td> <a href="updatestudent.jsp?id=<%=id%>">修改个人信息 </a> </td> </tr><tr> <td> <a href="/select_war_exploded/showLesson?id=<%=id%>">浏览学生选课信息 </a> </td> </tr> </table> </body> </html>
teacher.jsp
<%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/27 Time: 18:51 To change this template use File | Settings | File Templates. --%> <%String name = (String) request.getAttribute("name");%> <%String id = (String) request.getAttribute("id");%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>教师管理界面</title> </head> <body> 欢迎您<%=name%>老师 <table border="1" align="center"> <tr> <td> <a href="addlesson.jsp?name=<%=name%>" >添加课程信息 </a> </td> </tr><tr> <td> <a href="updateteacher.jsp?id=<%=id%>">修改个人信息 </a> </td> </tr><tr> <td> <a href="/select_war_exploded/showteacherlesson?name=<%=name%>">浏览学生选课信息 </a> </td> </tr> </table> </body> </html>
updateteacher.jsp
<%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/27 Time: 20:00 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%String id = request.getParameter("id");%> <html> <head> <title>修改个人信息</title> </head> <body> <form action="/select_war_exploded/updatestudent" method="get"> <table border="2" align="center"> <tr> <td>姓名</td> <td> <input type="text" name="name"> </td> </tr> <tr> <td>性别</td> <td> <select name="sex"> <option value="null">请选择性别</option> <option value="男">男</option> <option value="女">女</option> </select> </td> </tr> <tr> <td>班级</td> <td> <input type="text" name="banji"> </td> </tr> <tr> <td>专业</td> <td> <input type="text" name="zhuanye"> </td> </tr> <tr> <td>密码</td> <td> <input type="text" name="password"> </td> </tr> <tr> <td> <input type="hidden" value="<%=id%>" name="id"> <input type="submit" value="修改"> </td> </tr> </table> </form> </body> </html>
updatestudent.jsp
<%-- Created by IntelliJ IDEA. User: ACER-CN Date: 2021/11/27 Time: 19:25 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String id =request.getParameter("id");%> <html> <head> <title>修改个人信息</title> </head> <body> <%--<%=id%>--%> <form action="/select_war_exploded/updateteacher" method="get"> <table border="2" align="center"> <tr> <td>姓名</td> <td> <input type="text" name="name"> </td> </tr> <tr> <td>性别</td> <td> <select name="sex"> <option value="null">请选择性别</option> <option value="男">男</option> <option value="女">女</option> </select> </td> </tr> <tr> <td>学院</td> <td> <input type="text" name="xueyuan"> </td> </tr> <tr> <td>职称</td> <td> <select name="zhicheng"> <option value="null">请选择职称</option> <option value="教授">教授</option> <option value="副教授">副教授</option> <option value="讲师">讲师</option> <option value="助教">助教</option> </select> </td> </tr> <tr> <td>密码</td> <td> <input type="text" name="password"> </td> </tr> <tr> <td> <input type="hidden" value="<%=id%>" name="id"> <input type="submit" value="修改"> </td> </tr> </table> </form> </body> </html>