1 2 3 4

11.21

持续更新选课系统,今天发表了servlet层代码和dao层代码,整个选课系统就完毕了

dao层

package dao;
import java.sql.*;
public class DBUtil {
    //eshop为数据库名称,db_user为数据库用户名db_password为数据库密码
    public static String db_url = "jdbc:mysql://localhost:3306/schoolclass?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT";
    public static String db_user = "root";
    public static String db_password = "root1";

    public static Connection getConn() {
        Connection conn = null;
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(db_url, db_user, db_password);
            System.out.println("连接成功");
        } catch (Exception e) {
            System.out.println("连接失败");
            e.printStackTrace();
        }
        return conn;
    }
    
    public static void close(Statement state, Connection conn) {
        if(state!=null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();    
            }
        }    
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    
    public static void close(ResultSet rs, Statement state, Connection conn) {
        if(rs!=null) {
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(state!=null) {
            try {
                state.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if(conn!=null) {
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
package dao;
import java.sql.*;



import java.util.*;
import entity.School;
public class SchoolDaoImpl implements SchoolDao{
     Connection conn=(Connection)DBUtil.getConn();
     
     
     
     public boolean Stuadd(School school) {
        
        boolean flag=false;
        PreparedStatement ps=null;
        try {
            //String sql="insert into school values('"+school.getclassname()+"','"+school.getclassteacher()+"','"+school.getclassplace()+"')";
            
            ps=conn.prepareStatement("insert into student(stuID,stuname,stusex,stuclass,stumajor,stupassword)"+"values(?,?,?,?,?,?)");
            
            ps.setString(1,school.getStuID());
            ps.setString(2,school.getStuname());
            ps.setString(3,school.getStusex());
            ps.setString(4,school.getStuclass());
            ps.setString(5,school.getStumajor());
            ps.setString(6, school.getStupassword());
            int i=ps.executeUpdate();
            
            ps.close();
            conn.close();
            
            if(i>0) {
                flag = true;System.out.println("添加成功");
            }

        }catch(SQLException e) {
            e.printStackTrace();
        }
        return flag;    
    }
     
     public boolean Teacheradd(School school) {
            
            boolean flag=false;
            PreparedStatement ps=null;
            try {
                //String sql="insert into school values('"+school.getclassname()+"','"+school.getclassteacher()+"','"+school.getclassplace()+"')";
                
                ps=conn.prepareStatement("insert into teacher(teacherID,teachername,teachersex,teachercollege,teachercall,teacherpassword)"+"values (?,?,?,?,?,?)");
                
                ps.setString(1,school.getTeacherID());
                ps.setString(2,school.getTeachername());
                ps.setString(3,school.getTeachersex());
                ps.setString(4,school.getTeachercollege());
                ps.setString(5,school.getTeachercall());
                ps.setString(6, school.getTeacherpassword());
                int i=ps.executeUpdate();
                System.out.println("添加成功");
                ps.close();
                conn.close();
                
                if(i>0) {
                    flag = true;
                }

            }catch(SQLException e) {
                e.printStackTrace();
            }
            return flag;    
        }
     public boolean classadd(String teacherID,String teachername,String classID,String classname,String classnum) {
            
            boolean flag=false;
            PreparedStatement ps=null;
            try {
                //String sql="insert into school values('"+school.getclassname()+"','"+school.getclassteacher()+"','"+school.getclassplace()+"')";
                
                ps=conn.prepareStatement("insert into class(teacherID,teachername,classID,classname,classnum)"+"values (?,?,?,?,?)");
                ps.setString(1,teacherID);
                ps.setString(2,teachername);
                ps.setString(3,classID);
                ps.setString(4,classname);
                ps.setString(5,classnum);
                
                int i=ps.executeUpdate();
                
                ps.close();
                conn.close();
                
                if(i>0) {
                    flag = true;System.out.println("添加成功");
                }

            }catch(SQLException e) {
                e.printStackTrace();
            }
            return flag;    
        }
     
     public List<School>showclass(){
        List<School> list = new ArrayList<School>();
        try {
            System.out.println(1);
            String sql="select * from class";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            ResultSet rs = pstmt.executeQuery();
            System.out.println(2);
            while(rs.next()) {
                School school=new School();
                    school.setTeacherID(rs.getString("teacherID"));
                    school.setClassID(rs.getString("classID"));
                    school.setClassname(rs.getString("classname"));
                    school.setClassnum(rs.getString("classnum"));
                    school.setTeachername(rs.getString("teachername"));    
                    school.setClassnow(rs.getInt("classnow"));
                list.add(school);
            }
            rs.close();
            pstmt.close();
            conn.close();

        }catch(SQLException e) {
            e.printStackTrace();
        }
        
        return list;
    }
        
     
     
     public List<School>showteacher(String teacherID){
        List<School> list = new ArrayList<School>();
        try {
            String sql="select * from teacher where teacherID=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,teacherID);
            ResultSet rs = pstmt.executeQuery();
            while(rs.next()) {
                School school=new School();
                school.setTeacherID(rs.getString("teacherID"));
                System.out.println(teacherID);
                school.setTeacherpassword(rs.getString("teacherpassword"));
                school.setTeachername(rs.getString("teachername"));
                school.setTeachersex(rs.getString("teachersex"));
                school.setTeachercollege(rs.getString("teachercollege"));
                school.setTeachercall(rs.getString("teachercall"));
                list.add(school);
            }
            rs.close();
            pstmt.close();
            conn.close();
        }catch(SQLException e) {
            e.printStackTrace();
        }
        return list;
        
    }
     public String showteachername(String teacherID){
         String password=null;
            try {
                String sql="select * from teacher where teacherID=?";
                PreparedStatement pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,teacherID);
                ResultSet rs = pstmt.executeQuery();
                while(rs.next()) {
                    School school=new School();
                    
                    school.setTeacherpassword(rs.getString("teacherpassword"));
                    password=school.getTeacherpassword();
                    
                }
                rs.close();
                pstmt.close();
                conn.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }
            return password;
            
        }
     
     public String stushowname(String stuID){
         String password=null;
            try {
                String sql="select * from student where stuID=?";
                PreparedStatement pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,stuID);
                ResultSet rs = pstmt.executeQuery();
                while(rs.next()) {
                    School school=new School();
                    
                    school.setStupassword(rs.getString("stupassword"));
                    password=school.getStupassword();
                    
                }
                rs.close();
                pstmt.close();
                conn.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }
            return password;
            
        }
     
     public boolean teacherupdate(String teacherID,String teachername,String teachersex,String teachercollege,String teachercall) {
            
                boolean flag=false;
                try {
                    String sql="update teacher set teachername=?,teachersex=?,teachercollege=?,teachercall=?where teacherID=?";
                    PreparedStatement pstmt = conn.prepareStatement(sql);
                    pstmt.setString(5, teacherID);
                    pstmt.setString(1,teachername);
                    pstmt.setString(2,teachersex);
                    pstmt.setString(3,teachercollege);
                    pstmt.setString(4,teachercall);
                    int i = pstmt.executeUpdate();
                    
                    pstmt.close();
                    conn.close();
                    if(i>0) {flag = true;System.out.println("修改成功!!!");}
                }catch(SQLException e) {
               
                    e.printStackTrace();
                }

                return flag;
                
        }
     
     public List<School>stushow(String stuID){
            List<School> list = new ArrayList<School>();
            try {
                String sql="select * from student where stuID=?";
                PreparedStatement pstmt = conn.prepareStatement(sql);
                pstmt.setString(1,stuID);
                ResultSet rs = pstmt.executeQuery();
                while(rs.next()) {
                    School school=new School();
                    school.setStuID(rs.getString("stuID"));
                    System.out.println(stuID);
                    school.setStupassword(rs.getString("stupassword"));
                    school.setStuname(rs.getString("stuname"));
                    school.setStusex(rs.getString("stusex"));
                    school.setStuclass(rs.getString("stuclass"));
                    school.setStumajor(rs.getString("stumajor"));
                    list.add(school);
                }
                rs.close();
                pstmt.close();
                conn.close();
            }catch(SQLException e) {
                e.printStackTrace();
            }
            return list;
            
        }
    
     public boolean stuupdate(String stuID,String stuname,String stusex,String stuclass,String stumajor) {
            
         boolean flag=false;
         try {
             String sql="update student set stuname=?,stusex=?,stuclass=?,stumajor=?where stuID=?";
             PreparedStatement pstmt = conn.prepareStatement(sql);
             pstmt.setString(5, stuID);
             pstmt.setString(1,stuname);
             System.out.println(stuname+'2');
             pstmt.setString(2,stusex);
             pstmt.setString(3,stuclass);
             pstmt.setString(4,stumajor);
             int i = pstmt.executeUpdate();
             
             pstmt.close();
             conn.close();
             if(i>0) {flag = true;System.out.println("修改成功!!!");}
         }catch(SQLException e) {
        
             e.printStackTrace();
         }

         return flag;
         
 }

    public List<School> classshow() {
        // TODO 自动生成的方法存根
         List<School> list = new ArrayList<School>();
            try {
                System.out.println(1);
                String sql="select * from class";
                PreparedStatement pstmt = conn.prepareStatement(sql);
                ResultSet rs = pstmt.executeQuery();
                System.out.println(2);
                while(rs.next()) {
                    School school=new School();
                    school.setClassnow(rs.getInt("classnow"));
                    school.setClassnum(rs.getString("classnum"));
                    int now=school.getClassnow();
                    int num= Integer.parseInt(school.getClassnum());
                    if(now<num) {
                        school.setTeacherID(rs.getString("teacherID"));
                        school.setClassID(rs.getString("classID"));
                        school.setClassname(rs.getString("classname"));
                        school.setClassnum(rs.getString("classnum"));
                        school.setTeachername(rs.getString("teachername"));    
                        school.setClassnow(rs.getInt("classnow"));
                        
                    list.add(school);}
                    
                }
                rs.close();
                pstmt.close();
                conn.close();

            }catch(SQLException e) {
                e.printStackTrace();
            }
            
            return list;
        
    }

    public List<School> selectshow(String classID) {
        // TODO 自动生成的方法存根
        List<School> list = new ArrayList<School>();
        try {
            System.out.println(1);
            String sql="select * from class where classID=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,classID);
            ResultSet rs = pstmt.executeQuery();
            System.out.println(2);
            while(rs.next()) {
                School school=new School();
                    school.setTeacherID(rs.getString("teacherID"));
                    school.setClassID(rs.getString("classID"));
                    school.setClassname(rs.getString("classname"));
                    school.setClassnum(rs.getString("classnum"));
                    school.setTeachername(rs.getString("teachername"));    
                    school.setClassnow(rs.getInt("classnow"));
                list.add(school);
            }
            rs.close();
            pstmt.close();
            conn.close();

        }catch(SQLException e) {
            e.printStackTrace();
        }
        
        return list;
    }

    public boolean selectclass(School school) {
        // TODO 自动生成的方法存根
        boolean flag=false;
        PreparedStatement ps=null;
        try {
            //String sql="insert into school values('"+school.getclassname()+"','"+school.getclassteacher()+"','"+school.getclassplace()+"')";
            
            ps=conn.prepareStatement("insert into selectclass(stuID,stuname,stusex,stuclass,stumajor,teachername,classID,classname,classnow,classnum)"+"values(?,?,?,?,?,?,?,?,?,?)");
            
            ps.setString(1,school.getStuID());
            ps.setString(2,school.getStuname());
            ps.setString(3,school.getStusex());
            ps.setString(4,school.getStuclass());
            ps.setString(5,school.getStumajor());
            ps.setString(6, school.getTeachername());
            ps.setString(7, school.getClassID());
            ps.setString(8, school.getClassname());
            ps.setInt(9, school.getClassnow());
            ps.setString(10, school.getClassnum());
            int i=ps.executeUpdate();
            
            ps.close();
            conn.close();
            
            if(i>0) {
                flag = true;System.out.println("添加成功");
                
            }
            

        }catch(SQLException e) {
            e.printStackTrace();
        }
        return flag;    
    }

    public boolean updatenow(int classnow,String classID) {
        // TODO 自动生成的方法存根
        boolean flag=false;
        try {
            String sql="update class set classnow=? where classID=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            
            pstmt.setInt(1,classnow);
            pstmt.setString(2, classID);
            
            System.out.println(classnow);
            
            int i = pstmt.executeUpdate();
            
            pstmt.close();
            conn.close();
            if(i>0) {flag = true;System.out.println("修改成功!!!");}
        }catch(SQLException e) {
       
            e.printStackTrace();
        }

        return flag;
    }

    public List<School> stushowclass(String teacherID) {
        // TODO 自动生成的方法存根
        List<School> list = new ArrayList<School>();
        try {
            System.out.println(1);
            String sql="select * from class where teacherID=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,teacherID);
            ResultSet rs = pstmt.executeQuery();
            System.out.println(2);
            while(rs.next()) {
                School school=new School();
                    school.setTeacherID(rs.getString("teacherID"));
                    school.setClassID(rs.getString("classID"));
                    school.setClassname(rs.getString("classname"));
                    school.setClassnum(rs.getString("classnum"));
                    school.setTeachername(rs.getString("teachername"));    
                    school.setClassnow(rs.getInt("classnow"));
                list.add(school);
            }
            rs.close();
            pstmt.close();
            conn.close();

        }catch(SQLException e) {
            e.printStackTrace();
        }
        
        return list;
    }

    public List<School> showstudent(String classID) {
        // TODO 自动生成的方法存根
        List<School> list = new ArrayList<School>();
        try {
            System.out.println(1);
            String sql="select * from selectclass where classID=?";
            PreparedStatement pstmt = conn.prepareStatement(sql);
            pstmt.setString(1,classID);
            ResultSet rs = pstmt.executeQuery();
            System.out.println(2);
            while(rs.next()) {
                School school=new School();
                    school.setStuID(rs.getString("stuID"));
                    school.setStusex(rs.getString("stusex"));
                    school.setStuname(rs.getString("stuname"));
                    school.setStuclass(rs.getString("stuclass"));
                    school.setStumajor(rs.getString("stumajor"));
                    school.setClassname(rs.getString("classname"));
                list.add(school);
            }
            rs.close();
            pstmt.close();
            conn.close();

        }catch(SQLException e) {
            e.printStackTrace();
        }
        
        return list;
    }



    }

    



    

    
     
     
     

servelt层代码

 

 

package SchoolServlet;

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.SchoolDaoImpl;
import entity.School;
/**
 * Servlet implementation class SchoolAddServlet
 */
@WebServlet("/SchoolAddServlet")
public class SchoolAddServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SchoolAddServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        
        String method=request.getParameter("method");
        String stuID=request.getParameter("stuID");
        String stuname=request.getParameter("stuname");
        String stusex=request.getParameter("stusex");
        String stumajor=request.getParameter("stumajor");
        String stuclass=request.getParameter("stuclass");
        String stupassword=request.getParameter("stupassword");
        String teacherID=request.getParameter("teacherID");
        String teachername=request.getParameter("teachername");
        String teachersex=request.getParameter("teachersex");
        String teachercollege=request.getParameter("teachercollege");
        String teachercall=request.getParameter("teachercall");
        String teacherpassword=request.getParameter("teacherpassword");
        
        School school=new School();
        school.setStuclass(stuclass);
        school.setStuID(stuID);
        school.setStumajor(stumajor);
        school.setStuname(stuname);
        school.setStusex(stusex);
        school.setStupassword(stupassword);
        school.setTeachercall(teachercall);
        school.setTeachercollege(teachercollege);
        school.setTeacherID(teacherID);
        school.setTeachername(teachername);
        school.setTeachersex(teachersex);
        school.setTeacherpassword(teacherpassword);
        
        SchoolDaoImpl dao=new SchoolDaoImpl();
        if("stu".equals(method)) {
            try{
                System.out.println(1);
                dao.Stuadd(school);
                request.getRequestDispatcher("manage.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("添加失败");
                e.printStackTrace();
            }
        }
           
           
           if("teacher".equals(method)) {
               
               try{
                    //System.out.println(1);
                    dao.Teacheradd(school);
                    request.getRequestDispatcher("manage.jsp").forward(request, response);

                }catch(Exception e){
                    System.out.println("添加失败");
                    e.printStackTrace();
                }
           }
        
        
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package SchoolServlet;

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 javax.servlet.http.HttpSession;
import javax.websocket.Session;

import dao.SchoolDaoImpl;
import entity.School;

/**
 * Servlet implementation class SchoolServlet
 */
@WebServlet("/SchoolServlet")
public class SchoolServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SchoolServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        String ID=request.getParameter("username");
        HttpSession session=request.getSession();
        session.setAttribute("ID", ID);//存储数据
        request.setAttribute("ID", ID);
        String password=request.getParameter("password");
        String method=request.getParameter("profession");
        SchoolDaoImpl sd=new SchoolDaoImpl();
        if("1".equals(method)) {
            try{
                System.out.println(1);
                
                if("123456".equals(ID)&&"654321".equals(password)) {
                    response.getWriter().println("<script type='text/javascript'> alert('登录成功')</script>");
                request.getRequestDispatcher("manage.jsp").forward(request, response);}
                else {
                    response.getWriter().println("<script type='text/javascript'> alert('密码或账号错误')</script>");
                    request.getRequestDispatcher("school.jsp").forward(request, response);}
            }catch(Exception e){
                System.out.println("失败");
                e.printStackTrace();
            }
        }
        if("2".equals(method)) {
            try{
                System.out.println(1);
                
                String teacherpass=sd.showteachername(ID);
                
                    
                if(password.equals(teacherpass)) {
                    response.getWriter().println("<script type='text/javascript'> alert('登录成功')</script>");
                    request.getRequestDispatcher("teacher.jsp").forward(request, response);}
                else {
                    response.getWriter().println("<script type='text/javascript'> alert('账号或密码错误')</script>");
                    request.getRequestDispatcher("school.jsp").forward(request, response);}
                
                

            }catch(Exception e){
                System.out.println("失败");
                e.printStackTrace();
            }
        }
        if("3".equals(method)) {
            try{
                System.out.println(1);
                String stupass=sd.stushowname(ID);
                if(password.equals(stupass)) {
                    SchoolDaoImpl sd1=new SchoolDaoImpl();
                    java.util.List<School> list=sd1.stushow(ID);
                    
                    request.setAttribute("list", list);
                    response.getWriter().println("<script type='text/javascript'> alert('登录成功')</script>");
                    request.getRequestDispatcher("student.jsp").forward(request, response);}
                else {
                    response.getWriter().println("<script type='text/javascript'> alert('账号或密码错误')</script>");
                    request.getRequestDispatcher("school.jsp").forward(request, response);}
                

            }catch(Exception e){
                System.out.println("失败");
                e.printStackTrace();
            }
        }
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package SchoolServlet;

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.SchoolDaoImpl;
import entity.School;

/**
 * Servlet implementation class Studentservlet
 */
@WebServlet("/Studentservlet")
public class Studentservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Studentservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        
        String method=request.getParameter("method");
        String stuID=(String) request.getSession().getAttribute("stuID");
        String stuname=(String) request.getSession().getAttribute("stuname");
        System.out.println(stuname+'0');
        String stusex=(String) request.getSession().getAttribute("stusex");
        String stuclass=(String) request.getSession().getAttribute("stuclass");
        String stumajor=(String) request.getSession().getAttribute("stumajor");
        
        
        
        SchoolDaoImpl sd=new SchoolDaoImpl();
        if("show".equals(method)) {
            try{
                
                java.util.List<School> list=sd.stushow(stuID);
                
                request.setAttribute("list", list);
                System.out.println("listshow");
                request.getRequestDispatcher("studentupdate.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("未能正确显示出个人信息");
                e.printStackTrace();
            }
        }
        
        if("showclass".equals(method)) {
            try{
                
                java.util.List<School> list=sd.showclass();
                
                request.setAttribute("list", list);
                System.out.println("listshow");
                request.getRequestDispatcher("classshow.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("未能读出数据!");
                e.printStackTrace();
            }
            
            
        }
        
        
        if("update".equals(method)) {
            try{
            String stuID1=request.getParameter("stuID");
            String stuname1=request.getParameter("stuname");
            String stusex1=request.getParameter("stusex");
            String stuclass1=request.getParameter("stuclass");
            String stumajor1=request.getParameter("stumajor");
                System.out.println(1);
                System.out.println(stuname+'1');
                sd.stuupdate(stuID1,stuname1,stusex1,stuclass1,stumajor1);
                request.getRequestDispatcher("student.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("修改失败");
                e.printStackTrace();
            }
        }
        
        if("classshow".equals(method)) {
            try{
                System.out.println(1);
                System.out.println(stuname+'1');
                java.util.List<School> list=sd.classshow();
                
                request.setAttribute("list", list);
                
                request.getRequestDispatcher("selectshow.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("失败");
                e.printStackTrace();
            }
        }
        
        if("selectshow".equals(method)) {
            try{
                System.out.println(1);
                System.out.println(stuname+'1');
                String classID=request.getParameter("classID");
                java.util.List<School> list=sd.selectshow(classID);
                
                request.setAttribute("list", list);
                
                request.getRequestDispatcher("selectclass.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("失败");
                e.printStackTrace();
            }
        }
        
        if("selectclass".equals(method)) {
            try{
                School school=new School();
                String teachername=(String) request.getSession().getAttribute("teachername");
                String classID=(String) request.getSession().getAttribute("classID");
                String classname=(String) request.getSession().getAttribute("classname");
                String classnum=(String) request.getSession().getAttribute("classnum");
                int classnow=(int) request.getSession().getAttribute("classnow");
                System.out.println(classnow);
                school.setStuID(stuID);
                school.setStuname(stuname);
                school.setStusex(stusex);
                school.setStuclass(stuclass);
                school.setStumajor(stumajor);
                school.setClassID(classID);
                school.setClassname(classname);
                school.setTeachername(teachername);
                school.setClassnow(classnow);
                school.setClassnum(classnum);
                System.out.println(1);
                System.out.println(stuname+'1');
                sd.selectclass(school);
                
                int now=school.getClassnow();
                ++now;
                SchoolDaoImpl sd1=new SchoolDaoImpl();
                sd1.updatenow(now,classID);
                
                
                
                request.getRequestDispatcher("Studentservlet?method=classshow").forward(request, response);

            }catch(Exception e){
                System.out.println("失败");
                e.printStackTrace();
            }
        }
        
        
        
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
package SchoolServlet;

import java.awt.List;
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.SchoolDaoImpl;
import entity.School;

/**
 * Servlet implementation class Teacherservlet
 */
@WebServlet("/Teacherservlet")
public class Teacherservlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Teacherservlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //response.getWriter().append("Served at: ").append(request.getContextPath());
        
        String method=request.getParameter("method");
        String teacherID=(String) request.getSession().getAttribute("teacherID");
        String teachername=request.getParameter("teachername");
        String teachersex=request.getParameter("teachersex");
        String teachercollege=request.getParameter("teachercollege");
        String teachercall=request.getParameter("teachercall");
        String classID=request.getParameter("classID");
        String classname=request.getParameter("classname");
        String classnum=request.getParameter("classnum");
        
        SchoolDaoImpl sd=new SchoolDaoImpl();
        
        System.out.println(method+teacherID);
        
        if("show".equals(method)) {
            try{
                System.out.println("showteacher0");
                java.util.List<School> list=sd.showteacher(teacherID);
                System.out.println("showteacher1");
                request.setAttribute("list", list);
                System.out.println("listshow");
                request.getRequestDispatcher("teacherupdate.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("未能正确显示出个人信息");
                e.printStackTrace();
            }
        }
        if("showteacher".equals(method)) {
            try{
                System.out.println("showteacher0");
                String teacherID1=request.getParameter("teacherID");
                java.util.List<School> list=sd.showteacher(teacherID1);
                System.out.println("showteacher1");
                request.setAttribute("list", list);
                System.out.println("listshow");
                request.getRequestDispatcher("teachershow.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("未能正确显示出个人信息");
                e.printStackTrace();
            }
        }
        if("showname".equals(method)) {
            try{
                System.out.println("showteacher0");
                java.util.List<School> list=sd.showteacher(teacherID);
                System.out.println("showteacher1");
                request.setAttribute("list", list);
                System.out.println("listshow");
                request.getRequestDispatcher("classadd.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("未能正确显示出个人信息");
                e.printStackTrace();
            }
        }
        if("update".equals(method)) {
            try{
                System.out.println(1);
                
                sd.teacherupdate(teacherID,teachername,teachersex,teachercollege,teachercall);
                request.getRequestDispatcher("teacher.jsp").forward(request, response);

            }catch(Exception e){
                System.out.println("修改失败");
                e.printStackTrace();
            }
        }
        if("classadd".equals(method)) {
            try{
                System.out.println(1);
                String teachername1=(String) request.getSession().getAttribute("teachername");
                
                
                sd.classadd(teacherID,teachername1,classID,classname,classnum);
                request.getRequestDispatcher("teacher.jsp").forward(request, response);
                
            }catch(Exception e){
                System.out.println("添加失败");
                e.printStackTrace();
            }
        }
        
        if("name".equals(method)) {
            try{
                //System.out.println(1);
                java.util.List<School> list=sd.showteacher(teacherID);
                request.setAttribute("list", list);
                
                request.getRequestDispatcher("teacher.jsp").forward(request, response);
                
            }catch(Exception e){
                System.out.println("失败");
                e.printStackTrace();
            }
        }
        
        if("showclass".equals(method)) {
            try{
                //System.out.println(1);
                
                java.util.List<School> list=sd.stushowclass(teacherID);
                request.setAttribute("list", list);
                
                request.getRequestDispatcher("stushowclass.jsp").forward(request, response);
                
            }catch(Exception e){
                System.out.println("失败");
                e.printStackTrace();
            }
        }
        
        if("showstudent".equals(method)) {
            try{
                //System.out.println(1);
                
                java.util.List<School> list=sd.showstudent(classID);
                request.setAttribute("list", list);
                
                request.getRequestDispatcher("stushow.jsp").forward(request, response);
                
            }catch(Exception e){
                System.out.println("失败");
                e.printStackTrace();
            }
        }
        
        
        
        
        
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

 

posted @ 2020-11-21 23:22  小陈的太阳  阅读(92)  评论(0编辑  收藏  举报