选课系统

测试具体要求:

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发布:

要求可以实现在浏览器直接访问系统。

 

1.创建学生或教师的时候id和初始密码一致

2.在程序中使用cookie 来传递参数

3.对于身份的判定是在每一个账号后边加status 状态判定  

         1 为学生  2为教师 3 为管理员

4.对于选课存储时储存在了学生表的class 中,存储类型为课程编号,用String类型进行拼接,查询时进行模糊查询。

 

 

代码:

实体类

package com.Bean;

public class classes {
    private String name;
    private String teacher;
    private String pid;
    private String number;
    private String count;

    public classes(String name, String teacher, String pid, String number) {
        super();
        this.name = name;
        this.teacher = teacher;
        this.pid = pid;
        this.number = number;
    }
    public classes(String name, String teacher, String pid, String number, String count) {
        super();
        this.name = name;
        this.teacher = teacher;
        this.pid = pid;
        this.number = number;
        this.count = count;
    }
    public String getCount() {
        return count;
    }
    public void setCount(String count) {
        this.count = count;
    }
    public String getName() {
        return name;
    }
    public String getNumber() {
        return number;
    }
    public void setNumber(String number) {
        this.number = number;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getTeacher() {
        return teacher;
    }
    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }
    public String getPid() {
        return pid;
    }
    public void setPid(String pid) {
        this.pid = pid;
    }

}
package com.Bean;

public class premission {
    
    private String name;
    private String pwd;
    
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    
    

}
package com.Bean;

public class student {
    private String id;
    private String name;
    private String sex;
    private String classes;
    private String zhuanye;
    public String getId() {
        return id;
    }
    public student(String id, String name, String sex, String classes, String zhuanye) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.classes = classes;
        this.zhuanye = zhuanye;
    }
    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 getClasses() {
        return classes;
    }
    public void setClasses(String classes) {
        this.classes = classes;
    }
    public String getZhuanye() {
        return zhuanye;
    }
    public void setZhuanye(String zhuanye) {
        this.zhuanye = zhuanye;
    }
    

}
package com.Bean;

public class teacher {
    
    private String id;
    private String name;
    private String sex;
    private String xueyuan;
    private String zhicheng;
    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 teacher(String id, String name, String sex, String xueyuan, String zhicheng) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.xueyuan = xueyuan;
        this.zhicheng = zhicheng;
    }
    

}

dao层:

package com.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 com.Bean.classes;
import com.Bean.student;
import com.Bean.teacher;
import com.DBUtil.DBUtil;

public class Dao {
        //获取登陆用户的身份信息
    public int login(String username, String password) {
        
        Connection conn = DBUtil.getConn();
        Statement state =null;
        ResultSet rs = null;
        int flag=0;
        String sql = "select * from login where name='"+username+"' and  pwd = '"+password+"'";
        try {
            state = conn.createStatement();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        
        try {
            rs=state.executeQuery(sql);
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
         try {
            while(rs.next()) {
                flag=rs.getInt("status");
             }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return flag;
    }
    //在学生界面添加学生信息  --- 未读题
    public void addstudent(String id, String name, String sex, String classes, String zhuanye) throws Exception {
        Connection conn = DBUtil.getConn();
        Statement state =null;
        ResultSet rs = null;
        String sql="insert into student(pid,name,sex,classes,zhuanye) values('"+id+"','"+name+"','"+sex+"','"+classes+"','"+zhuanye+"')";
        state = conn.createStatement();
        state.executeUpdate(sql);
        DBUtil.close(state, conn);
    }
   //遍历所有的课程
    public List<classes> searchclasses() {
    
            //构建遍历函数
            List<classes>list =new ArrayList<>();
            //建立数据库链接
            Connection conn=DBUtil.getConn();
            //sql 语句 查询
            String sql="select * from classes";
            Statement pstmt = null;
            try {
                //执行sql语句
                pstmt = conn.createStatement();
                ResultSet rs=pstmt.executeQuery(sql);
                classes use=null;
                while(rs.next()) {
                    //遍历获取的信息
                    String name=rs.getString("name");
                    String teacher=rs.getString("teacher");
                    String id=rs.getString("pid");
                    String number=rs.getString("number");
                    String count=rs.getString("count");

                    use=new classes(name,teacher,id,number,count);
                    list.add(use);
                }
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            finally {
                DBUtil.close(pstmt, conn);
                }
            
            return list;
            }
    //增设新课程
    public void addclasses(String name, String teacher, String pid, String number,String count) throws SQLException {
        Connection conn = DBUtil.getConn();
        Statement state =null;
        String sql="insert into classes(name,teacher,pid,number,count) values('"+name+"','"+teacher+"','"+pid+"','"+number+"','"+count+"')";
        state = conn.createStatement();
        state.executeUpdate(sql);
        DBUtil.close(state, conn);
        
        
    }
    //遍历学生的信息
    public static ArrayList<student> getstudent(String pid) throws Exception{
        ArrayList<student>list =new ArrayList<>();
        Connection conn=DBUtil.getConn();
        Statement pstmt = null;
        String sql="select * from student where pid = '"+pid+"'";
        pstmt = conn.createStatement();
        ResultSet rs=pstmt.executeQuery(sql);
        student student=null;
        while(rs.next()) {
            //遍历获取的信息
            String id=rs.getString("pid");
            String name=rs.getString("name");
            String sex=rs.getString("sex");
            String classes=rs.getString("classes");
            String zy=rs.getString("zhuanye");
            student=new student(id,name,sex,classes,zy);
            list.add(student);
            System.out.println(id);
            System.out.println(name);
            System.out.println(sex);
            System.out.println(classes);
            System.out.println(zy);
        }
        return list;
        
    }
    //遍历教师的信息
    public static ArrayList<teacher> getteacher(String pid) throws Exception{
        ArrayList<teacher>list =new ArrayList<>();
        Connection conn=DBUtil.getConn();
        Statement pstmt = null;
        String sql="select * from teacher where pid = '"+pid+"'";
        pstmt = conn.createStatement();
        ResultSet rs=pstmt.executeQuery(sql);
        teacher teacher=null;
        while(rs.next()) {
            //遍历获取的信息
            String id=rs.getString("pid");
            String name=rs.getString("name");
            String sex=rs.getString("sex");
            String xy=rs.getString("xueyuan");
            String zc=rs.getString("zhicheng");
            teacher=new teacher(id,name,sex,xy,zc);
            list.add(teacher);
            System.out.println(id);
            System.out.println(name);
            System.out.println(sex);
            System.out.println(xy);
            System.out.println(zc);
        }
        return list;
        
    }
    
    
    
    
    
    
    //修改学生的个人信息  但是jsp学号不允许修改  在这里当作修改的索引
    public void updatestudent(String id, String name, String sex, String classes, String zhuanye) throws Exception {
        Connection conn = DBUtil.getConn();
        Statement state =null;
        String sql="update student set name='"+name+"',sex='"+sex+"',classes='"+classes+"',zhuanye='"+zhuanye+"'   where pid='"+id+"' ";
        state=conn.createStatement();
        state.executeUpdate(sql);
        
        DBUtil.close(state, conn);
    }
   // 修改教师个人信息
    public void updateteacher(String id, String name, String sex, String xueyuan, String zhicheng) throws Exception {
        Connection conn = DBUtil.getConn();
        Statement state =null;
        String sql="update teacher set name='"+name+"',sex='"+sex+"',xueyuan='"+xueyuan+"',zhicheng='"+zhicheng+"'   where pid='"+id+"' ";
        state=conn.createStatement();
        state.executeUpdate(sql);
        System.out.println("更新");
        DBUtil.close(state, conn);
        
    }

    public void addteacher(String id, String name, String sex, String classes, String zhuanye) throws Exception {
        Connection conn = DBUtil.getConn();
        Statement state =null;
        String sql="insert into teacher(pid,name,sex,xueyuan,zhicheng) values('"+id+"','"+name+"','"+sex+"','"+classes+"','"+zhuanye+"')";
        state = conn.createStatement();
        state.executeUpdate(sql);
        DBUtil.close(state, conn);
        
    }

    public void loginteacher(String id) throws Exception {
        Connection conn = DBUtil.getConn();
        Statement state =null;
        String sql="insert into login(name,pwd,status) values('"+id+"','"+id+"','"+2+"')";
        state = conn.createStatement();
        state.executeUpdate(sql);
        DBUtil.close(state, conn);
        
    }

    public void loginstudent(String id) throws Exception {
        Connection conn = DBUtil.getConn();
        Statement state =null;
        String sql="insert into login(name,pwd,status) values('"+id+"','"+id+"','"+1+"')";
        state = conn.createStatement();
        state.executeUpdate(sql);
        DBUtil.close(state, conn);
        
    }
    //选课
    public static void addclass(String name,String studentid,String id) throws Exception {
        Connection conn = DBUtil.getConn();
        Statement state =null;
        //给记录课程的数据库做标记  
        String sql="select * from classes where name = '"+name+"'";
        state = conn.createStatement();
        ResultSet rs=state.executeQuery(sql);
        int count=0;
        String a=null;
        //获取已经选取该课程的人数
        while(rs.next()) {
             a=rs.getString("count");
        }
        //将人数加一
        count=Integer.parseInt(a);
        count++;
        a=Integer.toString(count);
        //更新选课人数
        String sql1="update classes set count='"+count+"'   where name='"+name+"' ";
        state.executeUpdate(sql1);
        //获取选课学生的先前选课情况
        String sql2="select * from student where pid = '"+studentid+"'";
        String kecheng=null;
        state = conn.createStatement();
        ResultSet rs1=state.executeQuery(sql2);
        
        while(rs1.next()) {
            kecheng=rs1.getString("class");
        }
        //添加一个新的编号
        kecheng=kecheng+","+id;
        //给选课的学生做标记  记录学生的选课信息  在学生的数据库内记录
        String sql3="update student set class='"+kecheng+"'   where pid='"+studentid+"' ";
        state.executeUpdate(sql3);
        DBUtil.close(state, conn);
    }
  //通过姓名查询教师信息  不准确
    public static List<teacher> getteachers(String names) throws Exception{
        ArrayList<teacher>list =new ArrayList<>();
        Connection conn=DBUtil.getConn();
        Statement pstmt = null;
        String sql="select * from teacher where name = '"+names+"'";
        pstmt = conn.createStatement();
        ResultSet rs=pstmt.executeQuery(sql);
        teacher teacher=null;
        while(rs.next()) {
            //遍历获取的信息
            String id=rs.getString("pid");
            String name=rs.getString("name");
            String sex=rs.getString("sex");
            String xy=rs.getString("xueyuan");
            String zc=rs.getString("zhicheng");
            teacher=new teacher(id,name,sex,xy,zc);
            list.add(teacher);
            System.out.println("查看教师信息");
        }
        return list;
        
    }
    //查询教师开设的课程
    public static List<classes> getclass(String names) throws Exception {
        // TODO Auto-generated method stub
        
        List<classes> list=new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state =null;
        String sql="select * from classes where teacher like '%"+names+"%'";
        state = conn.createStatement();
        ResultSet rs=state.executeQuery(sql);
        classes cs=null;
        while(rs.next()) {
            String name=rs.getString("name");
            String teacher=rs.getString("teacher");
            String id=rs.getString("pid");
            String number=rs.getString("number");
            String count=rs.getString("count");
            cs=new classes(name,teacher,id,number,count);
            list.add(cs);
            System.out.println("教师查询选课结果");
        }
        return list;
    }
    //获取学生信息  通过id查找
    public static List<student> getstudents(String id) throws Exception {
        List<student> list=new ArrayList<>();
        Connection conn = DBUtil.getConn();
        Statement state =null;
        //模糊查询
        String sql="select * from student where class like '%"+id+"%'";
        state = conn.createStatement();
        ResultSet rs=state.executeQuery(sql);
        student s=null;
        while(rs.next()) {
            String pid=rs.getString("pid");
            String name=rs.getString("name");
            String sex=rs.getString("sex");
            String classes=rs.getString("classes");
            String zy=rs.getString("zhuanye");
            s=new student(pid,name,sex,classes,zy);
            list.add(s);
        }
        return list;
    }



}

servlet 类

package com.Servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

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 com.Dao.Dao;

/**
 * Servlet implementation class adminServlet
 */
@WebServlet("/adminServlet")
public class adminServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
    {
        req.setCharacterEncoding("UTF-8");
       
        String method = req.getParameter("method");
        if(method.equals("addstudent"))
        {
            try {
                addstudent(req,resp);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(method.equals("addteacher"))
        {
            try {
                addteacher(req,resp);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
            //添加教师信息
    private void addteacher(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        
        
        req.setCharacterEncoding("UTF-8");
        
        String id = req.getParameter("id");
         String name = req.getParameter("name");
         String sex = req.getParameter("sex");
         String classes = req.getParameter("classes");
         String zhuanye = req.getParameter("zhicheng");
         
         Dao dao=new Dao();
         System.out.println(zhuanye);
         dao.addteacher(id,name,sex,classes,zhuanye);
         dao.loginteacher(id);
         //添加完成重新刷新界面 
         req.getRequestDispatcher("addteacher.jsp").forward(req, resp);
    }
        //添加学生信息
    private void addstudent(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        // TODO Auto-generated method stub
        
        req.setCharacterEncoding("UTF-8");
        
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String classes = req.getParameter("classes");
        String zhuanye = req.getParameter("zhuanye");
       
        Dao dao=new Dao();
        dao.addstudent(id,name,sex,classes,zhuanye);
        dao.loginstudent(id);
        //跳转到新的界面
        req.getRequestDispatcher("addstudent.jsp").forward(req, resp);
    }

}
package com.Servlet;
/*
 * 登陆界面调用的Servlet
 */

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.Bean.premission;
import com.Dao.Dao;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
     protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
        {
            req.setCharacterEncoding("UTF-8");
           
            String method = req.getParameter("method");
            if(method.equals("login"))
            {
                login(req,resp);
            }
        }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String username = req.getParameter("user");
        String password = req.getParameter("password");
        System.out.println(username);
        System.out.println(password);
        premission p=new premission();
        p.setName(username);
        p.setPwd(password);
        Dao dao=new Dao();
        //获取登陆账号的身份权限信息
        int status=dao.login(username,password);
        //通过状态来跳转不同界面
        if(status==1) {
            System.out.println("学生");
            //设置cookie 作为边的参数
            Cookie cookie=new Cookie("student", username);
            cookie.setPath("/");
            //设置存活时间
            cookie.setMaxAge(60*60*24);
            resp.addCookie(cookie);
            req.getRequestDispatcher("student.jsp").forward(req, resp);
        }else if(status==2) {
            System.out.println("老师");
            //设置cookie 作为边的参数
            Cookie cookie=new Cookie("teacher", username);
            cookie.setPath("/");
            //设置存活时间
            cookie.setMaxAge(60*60*24);
            resp.addCookie(cookie);
            req.getRequestDispatcher("teacher.jsp").forward(req, resp);
        }else if(status==3) {
            System.out.println("管理员");
            //设置cookie 作为边的参数
            req.getRequestDispatcher("admin.jsp").forward(req, resp);
        }else if(status==0) {
            System.out.println("错误");
            req.getRequestDispatcher("error.jsp").forward(req, resp);
        }
        
    }

}
package com.Servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
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 com.Bean.classes;
import com.Dao.Dao;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
     protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
        {
            req.setCharacterEncoding("UTF-8");
           
            String method = req.getParameter("method");
            if(method.equals("add"))
            {
                try {
                    add(req,resp);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(method.equals("show"))
            {
                try {
                    show(req,resp);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(method.equals("update"))
            {
                try {
                    update(req,resp);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if(method.equals("addclasses")) {
                try {
                    addclass(req,resp);
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
        }
  //选课
    private void addclass(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("UTF-8");
        String name=req.getParameter("name");
        String studentid=req.getParameter("studentid");
        String id=req.getParameter("id");
        Dao.addclass(name,studentid,id);
        req.getRequestDispatcher("show.jsp").forward(req, resp);
    }
 // 修改学生信息
    private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        
            req.setCharacterEncoding("UTF-8");
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String sex = req.getParameter("sex");
            String classes = req.getParameter("classes");
            String zhuanye = req.getParameter("zhuanye");
            Dao dao=new Dao();
            dao.updatestudent(id,name,sex,classes,zhuanye);
            req.getRequestDispatcher("update1.jsp").forward(req, resp);
        
        
    }
     //展示所有的课程
    private void show(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // TODO Auto-generated method stub
        req.setCharacterEncoding("UTF-8");
        
        Dao dao=new Dao();
        List<classes> list= dao.searchclasses();
        req.setAttribute("list", list);
        req.getRequestDispatcher("show.jsp").forward(req, resp);
    }
    // 添加学生信息
    private void add(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        // TODO Auto-generated method stub
        try {
            req.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
     String id = req.getParameter("id");
     String name = req.getParameter("name");
     String sex = req.getParameter("sex");
     String classes = req.getParameter("classes");
     String zhuanye = req.getParameter("zhuanye");
  
     System.out.println(id);
     System.out.println(name);
     System.out.println(sex);
     System.out.println(classes);
     System.out.println(zhuanye);
     Dao dao=new Dao();
     
     
     dao.addstudent(id,name,sex,classes,zhuanye);
     
         //req.getRequestDispatcher("student.jsp").forward(req, resp);
     
     
    }

}
package com.Servlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
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 com.Bean.classes;
import com.Bean.student;
import com.Bean.teacher;
import com.Dao.Dao;

/**
 * Servlet implementation class TeacherServlet
 */
@WebServlet("/TeacherServlet")
public class TeacherServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    protected void service(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
    {
        req.setCharacterEncoding("UTF-8");
       
        String method = req.getParameter("method");
        if(method.equals("add"))
        {
            try {
                add(req,resp);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(method.equals("update"))
        {
            try {
                update(req,resp);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(method.equals("seacrch")) {
            try {
                search(req,resp);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
        if(method.equals("searchclass")) {
            try {
                searchclass(req,resp);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(method.equals("checkstudent")) {
            try {
                checkstudent(req,resp);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    //查询所选课程的学生信息
    private void checkstudent(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("UTF-8");
        String id=req.getParameter("id");
        //获取课程编号 据此查找学生 模糊
        List<student>list =Dao.getstudents(id);
        req.setAttribute("list", list);
        req.getRequestDispatcher("showstudentinfo.jsp").forward(req, resp);
    }
    //教师查询学生选课结果
    private void searchclass(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("UTF-8");
        String name=req.getParameter("name");
         
        List<classes> list=Dao.getclass(name);
        req.setAttribute("list", list);
        req.getRequestDispatcher("showselectclass.jsp").forward(req, resp);
    }
   //查询教师信息
    private void search(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("UTF-8");
        String name=req.getParameter("name");
        List<teacher> list=Dao.getteachers(name);
        req.setAttribute("list", list);
        req.getRequestDispatcher("showteacherinfo.jsp").forward(req, resp);
    }
    //修改教师个人信息
    private void update(HttpServletRequest req, HttpServletResponse resp) throws Exception {
        req.setCharacterEncoding("UTF-8");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String sex = req.getParameter("sex");
        String xueyuan = req.getParameter("xueyuan");
        String zhicheng = req.getParameter("zhicheng");
        Dao dao=new Dao();
        dao.updateteacher(id,name,sex,xueyuan,zhicheng);
        req.getRequestDispatcher("update2.jsp").forward(req, resp);
        
    }
    //添加新的课程
    private void add(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException, SQLException {
        // TODO Auto-generated method stub
        req.setCharacterEncoding("UTF-8");
        
        String name = req.getParameter("name");
        String  teacher= req.getParameter("teacher");
        String pid = req.getParameter("id");
        String number = req.getParameter("number");
        String count="0";
        Dao dao=new Dao();
        dao.addclasses(name,teacher,pid,number,count);
    }
}

DBUtil:

package com.DBUtil;
/**
 * 测试时主要要导入jar 
 */

import java.beans.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 建立数据库连接
 * @author cuixingyu
 *
 */
public class DBUtil {
    // 连接数据库 url路径和用户名 密码
    public static String db_url="jdbc:mysql://localhost:3306/db_database01?serverTimezone=UTC&characterEncoding=UTF-8&useSSL=false";
    public static String db_user="root";
    public static String db_password="123456";
    public static Connection getConn()  {
        Connection conn=null;
        try {
            // 数据库驱动加载
            Class.forName("com.mysql.cj.jdbc.Driver");
            try {
                //链接数据库
                conn=DriverManager.getConnection(db_url, db_user, db_password);
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("驱动链接加载成功!");
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //返回链接
        return conn;
        
    }
    
    //关闭函数
    public static void close(Statement state,Connection conn) {
        //只有状态和连接时,先关闭状态
        if(state!=null) {
            try {
                ((Connection) 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(PreparedStatement state,Connection conn) {
        if(state!=null) {
            try {
                ((Connection) 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 {
                ((Connection) 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(java.sql.Statement state, Connection conn) {
        // TODO Auto-generated method stub
        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();
            }
        }
    }
    //测试主函数  利用Java运行来测试
    @SuppressWarnings("static-access")
    public static void main(String[] args) throws SQLException {
        DBUtil dbu=new DBUtil();
        dbu.getConn();
    }
}

 

jsp界面省略,仅供参考使用。

posted @ 2019-12-20 17:14  嘉禾旧木  阅读(273)  评论(0编辑  收藏  举报