• 学生班级(双表)管理系统-增删改查—拓展—>网上书店项目的实现


    学生班级(双表)管理系统 (参考版:以前文博客代码为基础)

    1.1 标准思路:

    在这里插入图片描述

    1.2 代码目录:

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    1.3 标准代码:

    BaseDAO:

    package com.util;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class BaseDAO {
    
    	//四大金刚
    	//驱动类
    	private static final String DRIVER="com.mysql.cj.jdbc.Driver";
    	//连接地址
    	private static final String URL="jdbc:mysql://localhost:3306/0801_db?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
    	//用户名
    	private static final String USER="root";
    	//密码
    	private static final String PASSWORD="123456";
    
    	//获取连接
    	public static Connection getConnection(){
    
    		Connection con = null;
    
    		try{
    			//加载驱动类
    			Class.forName(DRIVER);
    			//获取连接
    			con = DriverManager.getConnection(URL,USER,PASSWORD);
    			
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}
    
    		return con;
    	}
    
    	//关闭数据库对象
    	public static void closeAll(Connection con,Statement st,ResultSet rs){
    		
    		if(rs!=null){
    			try{
    				rs.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    		if(st!=null){
    
    			try{
    				st.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    		if(con!=null){
    			try{
    				con.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    	}
    
    
    	//通用设置参数方法
    	public static void setParams(PreparedStatement pst,Object[] params){
    
    		if(params==null){
    			return;
    		}
    
    		for(int i=0;i> executeQuery(String sql,Object[] params) {
    
    		List> rows = new ArrayList<>();
    
    		Connection con = null;
    		PreparedStatement pst = null;
    		ResultSet rs = null;
    
    		try{
    			//获取连接	
    			con = getConnection();			
    			//获取命令对象
    			pst = con.prepareStatement(sql);
    			//设置参数
    			setParams(pst,params);
    			//执行查询
    			rs = pst.executeQuery();
    
    			//通过rs获取结果集的结构信息
    			ResultSetMetaData rsmd =  rs.getMetaData();
    			//获取结果集的列数
    			int colCount = rsmd.getColumnCount();
    
    			//遍历查询结果,并封装到List中
    			while(rs.next()){
    				//用Map存储当前行的各个列数据
    				Map map = new HashMap<>();
    				//循环获取每一列的信息
    				for(int i=1;i<=colCount;i++){
    					//获取列名(使用rsmd)
    					String colName = rsmd.getColumnLabel(i);
    					//获取列值(使用rs)
    					Object colVal = rs.getObject(i);
    					//将当前列存储到map中
    					map.put(colName,colVal);								
    				}
    				
    				//将遍历的当前行的数据存储到List中
    				rows.add(map);
    							
    			}
    
    
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}finally{
    			closeAll(con,pst,rs);
    		}
    		
    		return rows;
    
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173

    StudentInfo:

    package com.entity;
    
    import java.util.Date;
    
    /**
     * @author: hy
     * @create: 2022-08-01 10:01:14
     */
    public class StudentInfo {
        private Integer studentId;
        private String name;
        private String sex;
        private Date birthday;
        private Double tall;
    
        public StudentInfo() {
        }
    
        public StudentInfo(Integer studentId, String name, String sex, Date birthday, Double tall) {
            this.studentId = studentId;
            this.name = name;
            this.sex = sex;
            this.birthday = birthday;
            this.tall = tall;
        }
    
        public StudentInfo(String name, String sex, Date birthday, Double tall) {
            this.name = name;
            this.sex = sex;
            this.birthday = birthday;
            this.tall = tall;
        }
    
        public Integer getStudentId() {
            return studentId;
        }
    
        public void setStudentId(Integer studentId) {
            this.studentId = studentId;
        }
    
        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 Date getBirthday() {
            return birthday;
        }
    
        public void setBirthday(Date birthday) {
            this.birthday = birthday;
        }
    
        public Double getTall() {
            return tall;
        }
    
        public void setTall(Double tall) {
            this.tall = tall;
        }
    
        @Override
        public String toString() {
            return "StudentInfo{" +
                    "studentId=" + studentId +
                    ", name='" + name + '\'' +
                    ", sex='" + sex + '\'' +
                    ", birthday=" + birthday +
                    ", tall=" + tall +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    ClassInfo:

    package com.entity;
    
    /**
     * @author: hy
     * @create: 2022-08-01 10:52:43
     */
    public class ClassInfo {
        private Integer classId;
        private String className;
    
        public ClassInfo() {
        }
    
        public ClassInfo(Integer classId, String className) {
            this.classId = classId;
            this.className = className;
        }
    
        public ClassInfo(String className) {
            this.className = className;
        }
    
        public Integer getClassId() {
            return classId;
        }
    
        public void setClassId(Integer classId) {
            this.classId = classId;
        }
    
        public String getClassName() {
            return className;
        }
    
        public void setClassName(String className) {
            this.className = className;
        }
    
        @Override
        public String toString() {
            return "ClassInfo{" +
                    "classId=" + classId +
                    ", className='" + className + '\'' +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    IStudentInfoDAO:

    package com.dao;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * 数据访问层接口
     */
    public interface IStudentInfoDAO {
        List> listAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    StudentInfoDAOImpl:

    package com.dao.impl;
    
    import com.dao.IStudentInfoDAO;
    import com.util.BaseDAO;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * 数据访问层实现类
     * @author: hy
     * @create: 2022-08-01 10:03:24
     */
    public class StudentInfoDAOImpl implements IStudentInfoDAO {
        /**
         * 数据查询
         * @return
         */
        @Override
        public List> listAll() {
            String sql = "select studentId,name,sex,birthday,tall " +
                    "     from studentInfo ";
            return BaseDAO.executeQuery(sql,null);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    IClassInfoDAO:

    package com.dao;
    
    import com.entity.ClassInfo;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IClassInfoDAO {
        /**
         * 查询所有
         * @return
         */
        List> listAll();
        ClassInfo getByClassId(Integer classId);
    
        int insert(ClassInfo classInfo);
        int update(ClassInfo classInfo);
        int delete(Integer classId);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ClassInfoDAOImpl:

    package com.dao.impl;
    
    import com.dao.IClassInfoDAO;
    import com.entity.ClassInfo;
    import com.util.BaseDAO;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author: hy
     * @create: 2022-08-01 10:54:26
     */
    public class ClassInfoDAOImpl implements IClassInfoDAO {
        @Override
        public List> listAll() {
            String sql ="select classId,className from classInfo ";
            return BaseDAO.executeQuery(sql,null);
        }
    
        /**
         * 根据编号查询数据对象
         * @param classId
         * @return
         */
        @Override
        public ClassInfo getByClassId(Integer classId) {
            String sql ="select classId,className from classInfo " +
                    "    where classId = ? ";
            Object[] params = {classId};
            List> rows = BaseDAO.executeQuery(sql,params);
            if(rows.size()>0){
                Map map = rows.get(0);
                ClassInfo c = new ClassInfo(
                        (Integer)map.get("classId"),
                        (String)map.get("className")
                );
                System.out.println(c);
                return c;
            }
            return null;
        }
    
        @Override
        public int insert(ClassInfo classInfo) {
            String sql = "insert into classInfo" +
                    "     (className)" +
                    "     values" +
                    "     (?)";
            Object[] params = {classInfo.getClassName()};
            return BaseDAO.executeUpdate(sql,params);
        }
    
        @Override
        public int update(ClassInfo classInfo) {
            String sql = "update classInfo " +
                    "     set className = ? " +
                    "     where classId = ? ";
            Object[] params = {
                    classInfo.getClassName(),
                    classInfo.getClassId()
            };
            return BaseDAO.executeUpdate(sql,params);
        }
    
        @Override
        public int delete(Integer classId) {
            String sql = "delete from classInfo where classId = ? ";
            Object[] params = {classId};
            return BaseDAO.executeUpdate(sql,params);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73

    IStudentInfoService:

    package com.service;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * 业务层接口
     */
    public interface IStudentInfoService {
        /**
         * 查询所有数据
         * @return
         */
        List> listAll();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    StudentInfoServiceImpl:

    package com.service.impl;
    
    import com.dao.IStudentInfoDAO;
    import com.dao.impl.StudentInfoDAOImpl;
    import com.service.IStudentInfoService;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * 业务层实现类
     * @author: hy
     * @create: 2022-08-01 10:06:56
     */
    public class StudentInfoServiceImpl implements IStudentInfoService {
        /**
         * 创建数据访问层对象
         */
        private IStudentInfoDAO studentInfoDAO = new StudentInfoDAOImpl();
        /**
         * 查询实现方法
         * @return
         */
        @Override
        public List> listAll() {
            return studentInfoDAO.listAll();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    IClassInfoService:

    package com.service;
    
    import com.entity.ClassInfo;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IClassInfoService {
        //查询所有班级数据
        List> listAll();
        //根据编号查询班级对象
        ClassInfo getByClassId(Integer classId);
        int insert(ClassInfo classInfo);
        int update(ClassInfo classInfo);
        int delete(Integer classId);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    ClassInfoServiceImpl:

    package com.service.impl;
    
    import com.dao.IClassInfoDAO;
    import com.dao.impl.ClassInfoDAOImpl;
    import com.entity.ClassInfo;
    import com.service.IClassInfoService;
    
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author: hy
     * @create: 2022-08-01 10:55:41
     */
    public class ClassInfoServiceImpl implements IClassInfoService {
        //数据访问层对象
        private IClassInfoDAO classInfoDAO = new ClassInfoDAOImpl();
        @Override
        public List> listAll() {
            return classInfoDAO.listAll();
        }
    
        @Override
        public ClassInfo getByClassId(Integer classId) {
            return classInfoDAO.getByClassId(classId);
        }
    
        @Override
        public int insert(ClassInfo classInfo) {
            return classInfoDAO.insert(classInfo);
        }
    
        @Override
        public int update(ClassInfo classInfo) {
            return classInfoDAO.update(classInfo);
        }
    
        @Override
        public int delete(Integer classId) {
            return classInfoDAO.delete(classId);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    StudentInfoServlet:

    package com.servlet;
    
    import com.service.impl.StudentInfoServiceImpl;
    import com.service.IStudentInfoService;
    
    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;
    import java.util.Map;
    
    /**
     * @author: hy
     * @create: 2022-08-01 10:09:19
     */
    @WebServlet(urlPatterns = "/StudentInfoServlet")
    public class StudentInfoServlet extends HttpServlet {
    
        //定义业务对象
        IStudentInfoService studentInfoService = new StudentInfoServiceImpl();
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //查询数据
            List> studentList = studentInfoService.listAll();
            //设置数据
            req.setAttribute("studentList",studentList);
            //转发到页面
            req.getRequestDispatcher("/studentList.jsp").forward(req,resp);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    ClassInfoServlet:

    package com.servlet;
    
    import com.entity.ClassInfo;
    import com.service.IClassInfoService;
    import com.service.impl.ClassInfoServiceImpl;
    
    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;
    import java.util.Map;
    
    /**
     * @author: hy
     * @create: 2022-08-01 10:57:07
     */
    @WebServlet(urlPatterns = "/ClassInfoServlet/*")
    public class ClassInfoServlet extends HttpServlet {
        //定义业务层对象
        IClassInfoService classInfoService = new ClassInfoServiceImpl();
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //设置请求和响应对象的编码
            req.setCharacterEncoding("utf-8");
            resp.setContentType("text/html;charset=utf-8");
    
            //获取请求路径,截取请求处理的字符串
            //  uri: /lesson0801_crud/ClassInfoServlet/query
            String uri = req.getRequestURI();
            // process: query
            String process = uri.substring(uri.lastIndexOf("/")+1);
    
            //针对增删改查的请求做处理
            switch (process){
                case "query":
                    this.query(req,resp); //查询处理
                    break;
                case "toAdd":
                    this.toAdd(req,resp); //跳转到添加
                    break;
                case "add":
                    this.add(req,resp); //添加处理
                    break;
                case "toUpdate":
                    this.toUpdate(req,resp);//跳转到修改
                    break;
                case "update":
                    this.update(req,resp);//修改处理
                    break;
                case "delete":
                    this.delete(req,resp); //删除处理
                    break;
            }
    
    
        }
    
        private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取班级编号
            Integer classId = Integer.parseInt(req.getParameter("classId"));
            int count = classInfoService.delete(classId);
            if(count==1){
                //删除成功,重新显示
                this.query(req,resp);
            }else{
                //失败,跳转到失败页面
            }
        }
    
        private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取提交的表单数据
            Integer classId = Integer.parseInt(req.getParameter("classId"));
            String className = req.getParameter("className");
            ClassInfo classInfo = new ClassInfo(classId,className);
            //调用修改
            int count = classInfoService.update(classInfo);
            if(count==1){
                //修改成功,则重新显示
                this.query(req,resp);
            }else{
                //跳转到失败
            }
    
        }
    
        private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取要修改的编号:如果此时没有获取到编号,会爆出数字格式异常
            System.out.println("classId:"+req.getParameter("classId"));
            Integer classId = Integer.parseInt(req.getParameter("classId"));
            //根据编号,查询要修改的班级对象
            ClassInfo classInfo =  classInfoService.getByClassId(classId);
            //将要修改的班级对象,存储到request中,带到页面上,呈现出来
            req.setAttribute("classInfo",classInfo);
            //跳转到修改页面
            req.getRequestDispatcher("/classUpdate.jsp").forward(req,resp);
        }
    
        /**
         * 添加班级数据
         * @param req
         * @param resp
         */
        private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            //获取添加页面提交的数据
            String className = req.getParameter("className");
            ClassInfo ci = new ClassInfo(className);
            //构建班级对象
            int count = classInfoService.insert(ci);
    
            if(count==1){
                //添加成功,跳转到查询页面
                //resp.sendRedirect(req.getContextPath()+"/ClassInfoServlet/query");
                //直接调用查询逻辑
                this.query(req,resp);
            }else{
                //跳转到失败页面
                System.out.println("添加记录不等于1,失败了......");
            }
        }
    
        /**
         * 跳转到添加页面
         * @param req
         * @param resp
         */
        private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.getRequestDispatcher("/classAdd.jsp").forward(req,resp);
        }
    
        /**
         * 查询处理
         * @param req
         * @param resp
         */
        private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            List> classInfoList = classInfoService.listAll();
            req.setAttribute("classInfoList",classInfoList);
            req.getRequestDispatcher("/classList.jsp").forward(req,resp);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145

    studentList.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: henry
      Date: 2022/8/1
      Time: 10:13
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
    
    
        Title
    
    
        el:只负责数据的显示
        jstl: 分支,循环
    ${studentList}
    学号 姓名 性别 生日 身高
    ${stu.studentId} ${stu.name} ${stu.sex} ${stu.birthday} ${stu.tall}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    index.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: henry
      Date: 2022/8/1
      Time: 9:56
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
      
        $Title$
      
      
        学生信息
        班级信息
      
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    classList.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: henry
      Date: 2022/8/1
      Time: 10:58
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
    
    
        Title
    
    
        班级数据:${classInfoList}
        
    添加
    班级编号 班级名称 操作
    ${c.classId} ${c.className} 修改 删除
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49

    classAdd.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: henry
      Date: 2022/8/1
      Time: 11:20
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
        
    班级名称
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    classUpdate.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: henry
      Date: 2022/8/1
      Time: 11:20
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
    
        ${classInfo}
        
    班级名称
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    学生班级(双表)管理系统 (完整版)

    标准思路:

    在这里插入图片描述

    创建数据库:

    在这里插入图片描述

    创建数据表:

    在这里插入图片描述

    页面实现效果:

    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

    控制台结果展示:

    在这里插入图片描述

    导包(略,后面会写一个完整的博文)

    在这里插入图片描述

    代码目录整体布局:

    在这里插入图片描述

    在这里插入图片描述
    在这里插入图片描述

    代码部分:

    创建数据库:

    #创建数据库
    create database 70730_db
    default character set utf8mb4 #设置字符集
    default collate utf8mb4_general_ci #设置排序规则 
    
    • 1
    • 2
    • 3
    • 4

    创建数据表:

    #创建学生表
    create table student
    (
    	stuId int primary key auto_increment,
    	stuName varchar(20),
    	stuSex varchar(2),
    	stuBirthday date,
    	stuTall int
    );
    
    
    select * from student;
    
    insert into student
    (stuName,stuSex,stuBirthday,stuTall)
    values
    ('张三','男','1998-09-09',170),
    ('李四','女','2008-08-08',180);
    
    #创建班级表
    create table class
    (
    	classId int primary key auto_increment,
    	className varchar(20)
    );
    
    insert into class
    (className)
    values 
    ('菜鸟1班'),
    ('菜鸟2班');
    
    select * from class;
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    BaseDAO:

    package com.util;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    public class BaseDAO {
    
    	//四大金刚
    	//驱动类
    	private static final String DRIVER="com.mysql.cj.jdbc.Driver";
    	//连接地址
    	private static final String URL="jdbc:mysql://localhost:3306/70730_db?useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
    	//用户名
    	private static final String USER="root";
    	//密码
    	private static final String PASSWORD="123456";
    
    	//获取连接
    	public static Connection getConnection(){
    
    		Connection con = null;
    
    		try{
    			//加载驱动类
    			Class.forName(DRIVER);
    			//获取连接
    			con = DriverManager.getConnection(URL,USER,PASSWORD);
    			
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}
    
    		return con;
    	}
    
    	//关闭数据库对象
    	public static void closeAll(Connection con,Statement st,ResultSet rs){
    		
    		if(rs!=null){
    			try{
    				rs.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    		if(st!=null){
    
    			try{
    				st.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    		if(con!=null){
    			try{
    				con.close();
    			}catch(Exception ex){
    				ex.printStackTrace();
    			}
    			
    		}
    
    	}
    
    
    	//通用设置参数方法
    	public static void setParams(PreparedStatement pst,Object[] params){
    
    		if(params==null){
    			return;
    		}
    
    		for(int i=0;i> executeQuery(String sql,Object[] params) {
    
    		List> rows = new ArrayList<>();
    
    		Connection con = null;
    		PreparedStatement pst = null;
    		ResultSet rs = null;
    
    		try{
    			//获取连接	
    			con = getConnection();			
    			//获取命令对象
    			pst = con.prepareStatement(sql);
    			//设置参数
    			setParams(pst,params);
    			//执行查询
    			rs = pst.executeQuery();
    
    			//通过rs获取结果集的结构信息
    			ResultSetMetaData rsmd =  rs.getMetaData();
    			//获取结果集的列数
    			int colCount = rsmd.getColumnCount();
    
    			//遍历查询结果,并封装到List中
    			while(rs.next()){
    				//用Map存储当前行的各个列数据
    				Map map = new HashMap<>();
    				//循环获取每一列的信息
    				for(int i=1;i<=colCount;i++){
    					//获取列名(使用rsmd)
    					String colName = rsmd.getColumnLabel(i);
    					//获取列值(使用rs)
    					Object colVal = rs.getObject(i);
    					//将当前列存储到map中
    					map.put(colName,colVal);								
    				}
    				
    				//将遍历的当前行的数据存储到List中
    				rows.add(map);
    							
    			}
    
    
    		}catch(Exception ex){
    			ex.printStackTrace();
    		}finally{
    			closeAll(con,pst,rs);
    		}
    		
    		return rows;
    
    	}
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173

    Student:

    package com.entity;
    
    import java.util.Date;
    
    public class Student {
        private Integer stuId;
        private String stuName;
        private String stuSex;
        private Date stuBirthday;
        private Integer stuTall;
    //    stuId,stuName,stuSex,stuBirthday,stuTall
    
        public Student() {
        }
    
        public Student(Integer stuId, String stuName, String stuSex, Date stuBirthday, Integer stuTall) {
            this.stuId = stuId;
            this.stuName = stuName;
            this.stuSex = stuSex;
            this.stuBirthday = stuBirthday;
            this.stuTall = stuTall;
        }
    
        public Student(String stuName, String stuSex, Date stuBirthday, Integer stuTall) {
            this.stuName = stuName;
            this.stuSex = stuSex;
            this.stuBirthday = stuBirthday;
            this.stuTall = stuTall;
        }
    
        public Integer getStuId() {
            return stuId;
        }
    
        public void setStuId(Integer stuId) {
            this.stuId = stuId;
        }
    
        public String getStuName() {
            return stuName;
        }
    
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
    
        public String getStuSex() {
            return stuSex;
        }
    
        public void setStuSex(String stuSex) {
            this.stuSex = stuSex;
        }
    
        public Date getStuBirthday() {
            return stuBirthday;
        }
    
        public void setStuBirthday(Date stuBirthday) {
            this.stuBirthday = stuBirthday;
        }
    
        public Integer getStuTall() {
            return stuTall;
        }
    
        public void setStuTall(Integer stuTall) {
            this.stuTall = stuTall;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "stuId=" + stuId +
                    ", stuName='" + stuName + '\'' +
                    ", stuSex='" + stuSex + '\'' +
                    ", stuBirthday=" + stuBirthday +
                    ", stuTall=" + stuTall +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82

    ClassInfo:

    package com.entity;
    
    public class ClassInfo {
        private Integer classId;
        private String className;
    
        public ClassInfo() {
        }
    
        public ClassInfo(String className) {
            this.className = className;
        }
    
        public ClassInfo(Integer classId, String className) {
            this.classId = classId;
            this.className = className;
        }
    
        public Integer getClassId() {
            return classId;
        }
    
        public void setClassId(Integer classId) {
            this.classId = classId;
        }
    
        public String getClassName() {
            return className;
        }
    
        public void setClassName(String className) {
            this.className = className;
        }
    
        @Override
        public String toString() {
            return "Class{" +
                    "classId=" + classId +
                    ", className='" + className + '\'' +
                    '}';
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43

    IStudentsDAO:

    package com.dao;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IStudentsDAO {
        List> listAll();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    StudentsDAOImpl:

    package com.dao.impl;
    
    import com.dao.IStudentsDAO;
    import com.util.BaseDAO;
    
    import java.util.List;
    import java.util.Map;
    
    public class StudentsDAOImpl implements IStudentsDAO {
        @Override
        public List> listAll() {
            String sql="select * from student";
            return BaseDAO.executeQuery(sql,null);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    IClassDAO:

    package com.dao;
    
    import com.entity.ClassInfo;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IClassDAO {
        List> listAll();
        ClassInfo getByClassId(Integer classId);
    
        int insert(ClassInfo classInfo);
        int update(ClassInfo classInfo);
        int delete(Integer classId);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ClassDAOImpl:

    package com.dao.impl;
    
    import com.dao.IClassDAO;
    import com.entity.ClassInfo;
    import com.util.BaseDAO;
    
    import java.util.List;
    import java.util.Map;
    
    public class ClassDAOImpl implements IClassDAO {
        @Override
        public ClassInfo getByClassId(Integer classId) {
            String sql="select classId,className from class"+
                    "   where classId=?";
            Object[] params={classId};
            List> rows = BaseDAO.executeQuery(sql,params);
            if(rows.size()>0){
                Map map= rows.get(0);
    
                ClassInfo classInfo=new ClassInfo(
                        (Integer) map.get("classId"), //注意强转化 get得到的是Object类型(鼠标放在get上看)
                        (String) map.get("className")
                );
                System.out.println(classInfo);
                return classInfo;
            }
            return null;
        }
    
        @Override
        public List> listAll() {
            String sql="select * from class";
            return BaseDAO.executeQuery(sql,null);
        }
    
        @Override
        public int insert(ClassInfo classInfo) {
            String sql="insert into class"+
                    "   (className)"+
                    "   values"+
                    "   (?)";
            Object[] params={classInfo.getClassName()};
            return BaseDAO.executeUpdate(sql,params);
        }
    
        @Override
        public int update(ClassInfo classInfo) {
            String sql="update class"+
                    "   set className=?"+
                    "   where classId=?";
            Object[] params={
                    classInfo.getClassName(),
                    classInfo.getClassId()
            };
            return BaseDAO.executeUpdate(sql,params);
        }
    
        @Override
        public int delete(Integer classId) {
            String sql="delete from class"+  //注意delete写法
                    "   where classId=?";
            Object[] params={classId};
            return BaseDAO.executeUpdate(sql,params);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66

    IStudentsService:

    package com.service;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IStudentsService {
        List> listAll();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    StudentsServiceImpl:

    package com.service.impl;
    
    import com.dao.IStudentsDAO;
    import com.dao.impl.StudentsDAOImpl;
    import com.service.IStudentsService;
    
    import java.util.List;
    import java.util.Map;
    
    public class StudentsServiceImpl implements IStudentsService {
        IStudentsDAO studentsDAO=new StudentsDAOImpl();
        @Override
        public List> listAll() {
            return studentsDAO.listAll();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    IClassService:

    package com.service;
    
    import com.entity.ClassInfo;
    
    import java.util.List;
    import java.util.Map;
    
    public interface IClassService {
        List> listAll();
        ClassInfo getByClassId(Integer classId);
    
        int insert(ClassInfo classInfo);
        int update(ClassInfo classInfo);
        int delete(Integer classId);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    ClassServiceImpl:

    package com.service.impl;
    
    import com.dao.IClassDAO;
    import com.dao.impl.ClassDAOImpl;
    import com.entity.ClassInfo;
    import com.service.IClassService;
    
    import java.util.List;
    import java.util.Map;
    
    public class ClassServiceImpl implements IClassService {
        IClassDAO classDAO=new ClassDAOImpl();
    
        @Override
        public ClassInfo getByClassId(Integer classId) {
            return classDAO.getByClassId(classId);
        }
    
        @Override
        public List> listAll() {
            return classDAO.listAll();
        }
    
        @Override
        public int insert(ClassInfo classInfo) {
            return classDAO.insert(classInfo);
        }
    
        @Override
        public int update(ClassInfo classInfo) {
            return classDAO.update(classInfo);
        }
    
        @Override
        public int delete(Integer classId) {
            return classDAO.delete(classId);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    StudentInfoServlet:

    package com.servlet;
    
    import com.service.IStudentsService;
    import com.service.impl.StudentsServiceImpl;
    
    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;
    import java.util.Map;
    
    @WebServlet(urlPatterns = "/StudentInfoServlet")
    public class StudentInfoServlet extends HttpServlet {
        IStudentsService studentsService=new StudentsServiceImpl();
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            List> studentsList=studentsService.listAll();
            req.setAttribute("studentsList",studentsList);
            req.getRequestDispatcher("/studentsList.jsp").forward(req,resp);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    ClassInfoServlet:

    package com.servlet;
    
    import com.entity.ClassInfo;
    import com.service.IClassService;
    import com.service.impl.ClassServiceImpl;
    
    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;
    import java.util.Map;
    
    @WebServlet(urlPatterns = "/ClassInfoServlet/*")
    public class ClassInfoServlet extends HttpServlet {
        IClassService classService=new ClassServiceImpl();
    
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
    
            String uri=req.getRequestURI();
            String process=uri.substring(uri.lastIndexOf("/")+1);
            System.out.println("截取字段:"+process);
    
            switch (process){
                case "query":
                    this.query(req,resp);
                    break;
                case "toAdd":
                    this.toAdd(req,resp);
                    break;
                case "add":
                    this.add(req,resp);
                    break;
                case "toUpdate":
                    this.toUpdate(req,resp);
                    break;
                case "update":
                    this.update(req,resp);
                    break;
                case "delete":
                    this.delete(req,resp);
                    break;
            }
        }
    
        private void query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            List> classList=classService.listAll();
    
            req.setAttribute("classList",classList);
            req.getRequestDispatcher("/classList.jsp").forward(req,resp);
        }
    
        private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.getRequestDispatcher("/classAdd.jsp").forward(req,resp);
        }
    
        private void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String className=req.getParameter("className"); //注意 设置值关键字 从输入框获取值的关键字
            ClassInfo ci=new ClassInfo(className);
    
            int count=classService.insert(ci);
    
            if(count==1){
                System.out.println("插入成功!");
                this.query(req,resp);
            }else{
                System.out.println("插入失败!");
            }
        }
    
        private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Integer classId = Integer.parseInt(req.getParameter("classId"));
            //根据编号,查询要修改的班级对象
            ClassInfo classInfo =  classService.getByClassId(classId);
            //将要修改的班级对象,存储到request中,带到页面上,呈现出来
            req.setAttribute("classInfo",classInfo);
            //跳转到修改页面
            req.getRequestDispatcher("/classUpdate.jsp").forward(req,resp);
    
            req.setAttribute("classInfo",classInfo);
            req.getRequestDispatcher("/classUpdate.jsp").forward(req,resp);
        }
    
        private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Integer classId = Integer.parseInt(req.getParameter("classId"));
            String className = req.getParameter("className");
            ClassInfo classInfo = new ClassInfo(classId,className);
            //调用修改
            int count = classService.update(classInfo);
            if(count==1){
                //修改成功,则重新显示
                this.query(req,resp);
            }else{
                //跳转到失败
            }
    
        }
    
        private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            Integer classId= Integer.parseInt(req.getParameter("classId"));
            int count =classService.delete(classId);
            if(count==1){
                System.out.println("删除成功!");
                this.query(req,resp);
            }else{
                System.out.println("删除失败!");
            }
        }
    
    
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117

    index.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: 33154
      Date: 2022/8/1
      Time: 20:26
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
      
        $Title$
      
      
      $END$
      学生信息
      班级信息
     
      
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    studentsList.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: 33154
      Date: 2022/8/1
      Time: 20:57
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    
        Title
        <%=request.getAttribute("studentsList")%>
        
    ${studentsList}
    编号 姓名 性别 生日 身高
    ${stu.stuId} ${stu.stuName} ${stu.stuSex} ${stu.stuBirthday} ${stu.stuTall}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    classList.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: 33154
      Date: 2022/8/2
      Time: 1:18
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
    
        Title
        <%=request.getAttribute("classList")%>
    
    
        班级数据:${classList}
        
    添加
    班级编号 班级名称 操作
    ${clas.classId} ${clas.className} 修改 删除
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    classAdd.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: 33154
      Date: 2022/8/2
      Time: 2:08
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
        
    班级名称
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    classUpdate.jsp:

    <%--
      Created by IntelliJ IDEA.
      User: 33154
      Date: 2022/8/2
      Time: 2:56
      To change this template use File | Settings | File Templates.
    --%>
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    
    
        Title
    
    
        ${classInfo}
        
    班级名称
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    // A code block
    var foo = 'bar';
    
    • 1
    • 2
    // A code block
    var foo = 'bar';
    
    • 1
    • 2
  • 相关阅读:
    技术分享 | SQL 优化:ICP 的缺陷
    数据结构----线性表之顺序表
    php毕业设计美食菜谱网站
    联想G50笔记本直接使用F键功能(F1~F12)需要在BIOS设置关闭热键功能可以这样操作!
    Spring修炼之路(5)整合MyBatis和事务
    spring security auth2.0实现
    Altium Designer实用系列(一)----原理图导入PCB、PCB板子外形、多层板绘制等
    前端技术面试核心问题(持续更新)
    springboot+vue+java月子会所产后康复护理系统
    【EI会议征稿】JPCS独立出版-第五届新材料与清洁能源国际学术会议(ICAMCE 2024)
  • 原文地址:https://blog.csdn.net/Liu_wen_wen/article/details/126108242