当前位置: 首页 > article >正文

解决stuid项目类爆炸问题

文章目录

  • 目录结构
  • 处理类爆炸问题
  • 功能测试

目录结构

将同一个业务的后端程序整合到一个类中,也是MVC 分层,的雏形
分类 整合
在这里插入图片描述

处理类爆炸问题

package com.yanyu;

import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import resources.DBUtil;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author yanyu666_508200729@qq.com
 * @Date 2024/11/26 21:53
 * @description:解决类爆炸问题
 * 1.模拟  HttpServlet 的模板设计思想
 * 2.注解式开发
 * 3. 在原项目   stuid   基础 上开发,并修改号对应的  项目名字
 */
@WebServlet({"/list","/modifyStuno","/modify","/delete","/add"})
public class StudentService extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取  servlet  对象的路径   /web2/list    web2  项目名字,   /list
        String servletPath = request.getServletPath();//    /list

        if ("/list".equals(servletPath)) {
//           利用已知道的常量来调研方法,避开空引用异常
//            将  浏览器的请求对象带入
            doList(request, response);
        } else if ("/add".equals(servletPath)) {
            doAdd(request,response);//   alt   enter

        }else if ("/delete".equals(servletPath)){
            doDel(request,response);//   不要设置为  doDelete()   会与  HttpServlet 的   doDelete()
        } else if ("/modifyStuno".equals(servletPath)) {
            doModifyStuno(request,response);
        } else if ("/modify".equals(servletPath)) {
            doModify(request,response);

        }
    }

    private void doModify(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        String stuid = request.getParameter("stuid");
        String zhuanye = request.getParameter("zhuanye");
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            connection =  DBUtil.getConnection();
            connection.setAutoCommit(false);
            String sql = "update student set name = '"+name+"',zhuanye = '"+zhuanye+"' where stuid = '"+stuid+"'";
            statement = connection.createStatement();
            statement.executeUpdate(sql);
            connection.commit();
        } catch (SQLException e) {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(rs,statement,connection);
        }
        response.sendRedirect("/web2/list");
    }

    private void doModifyStuno(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
//        获取待续该学生的学号
        String stuno = request.getParameter("stuno");
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);
//            获取 该学生的信息
            String sql = "select * from student where stuid = '" + stuno + "'";
            statement = connection.createStatement();
            rs = statement.executeQuery(sql);
            while (rs.next()) {
                String name = rs.getString("name");
//                rs.getString("stuid");
                String zhuanye = rs.getString("zhuanye");
                out.print("<!DOCTYPE html>");
                out.print("<html lang='en'>");
                out.print("<head>");
                out.print("    <meta charset='UTF-8'>");
                out.print("    <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
                out.print("    <title>Document</title>");
                out.print("</head>");
                out.print("<body>");
                out.print("    <h1>修改学生</h1>");
//                设置  修改后的学生信息
                out.print("    <form action='/web2/modify'>");
                out.print("                    原姓名:"+name+"");
                out.print("                    <br>");
                out.print("            原学号:"+stuno+"");
                out.print("                    <br>");
                out.print("                    原专业:"+zhuanye+"");
                out.print("                    <br>");
                out.print("            姓名:<input type='text' name='name'>");
                out.print("        <br>");
//                修改前后,学号是不变的,所以要把学号拼接到   学号输入框   ,并将输入框设置为  只读不可修改
                out.print("                    学号:<input type='text' name='stuid' value='"+stuno+"' readonly>");
                out.print("        <br>");
                out.print("                    专业:<input type='text' name='zhuanye' >");
                out.print("        <br><input type='submit' value='确认修改'>");
                out.print("    </form>");
                out.print("</body>");
                out.print("</html>");

            }
            connection.commit();
        } catch (SQLException e) {
            if (connection != null) {
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(rs,statement,connection);
        }
    }

    private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
//        获取待续该学生的学号
        String delno = request.getParameter("delno");
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);
            String sql = "delete from student where stuid = '"+delno+"'";
            statement = connection.createStatement();
            statement.execute(sql);

            connection.commit();
        } catch (SQLException e) {
            if (connection != null) {
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(rs,statement,connection);
        }
        response.sendRedirect("/web2/list");
    }

    private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String name = request.getParameter("name");
        String stuid = request.getParameter("stuid");
        String zhuanye = request.getParameter("zhuanye");


        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            connection =  DBUtil.getConnection();
            connection.setAutoCommit(false);
            String sql = "insert into student(name,stuid,zhuanye) values('"+name+"','"+stuid+"','"+zhuanye+"') ";
            statement = connection.createStatement();
            statement.execute(sql);


            connection.commit();
        } catch (SQLException e) {
            if (connection != null) {
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        }finally {
            DBUtil.close(rs,statement,connection);
        }
        response.sendRedirect("/web2/list");
    }

    private void doList(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        处理整个查询业务   IO异常交给tomcat 处理
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        Connection connection = null;
        Statement statement = null;
        ResultSet rs = null;
        try {
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);
            String sql = "select * from student";
            statement = connection.createStatement();
            rs = statement.executeQuery(sql);
            out.print("<!DOCTYPE html>");
            out.print("<html lang='en'>");
            out.print("<head>");
            out.print("    <meta charset='UTF-8'>");
            out.print("    <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
            out.print("    <title>Document</title>");
            out.print("</head>");
            out.print("<body>");
            out.print("    <h1>学生信息</h1>");
            out.print("    <table border='1px' width='600px'>");
            out.print("        <tr>");
            out.print("            <td>姓名</td>");
            out.print("            <td>学号</td>");
            out.print("            <td>专业</td>");
            out.print("            <td>操作</td>");
            out.print("        </tr>");
            while (rs.next()) {
                String name = rs.getString("name");
                String stuid = rs.getString("stuid");
                String zhuanye = rs.getString("zhuanye");


                out.print("        <tr></tr>");
                out.print("            <td>" + name + "</td>");
                out.print("            <td>" + stuid + "</td>");
                out.print("            <td>" + zhuanye + "</td>");
                out.print("            <td>");
                out.print("                <a href='./add.html'>新增</a>");
//               发送修改学生信息之前   ,查询学生原来的信息        根据  stuid  在修改前后不变的原则
                out.print("                <a href='/web2/modifyStuno?stuno=" + stuid + "'>修改</a>");
//               根据  stuid   去限制  where  进行学生的  删除操作
                out.print("                <a href='/web2/delete?delno=" + stuid + "'>删除</a>");
                out.print("            </td>");
                out.print("        </tr>");

            }


            out.print("    </table>");
            out.print("</body>");
            out.print("</html>");


            connection.commit();
        } catch (SQLException e) {
            if (connection != null) {
                try {
                    connection.rollback();
                } catch (SQLException ex) {
                    throw new RuntimeException(ex);
                }
            }
            throw new RuntimeException(e);
        } finally {
            DBUtil.close(rs, statement, connection);
        }
    }
}

功能测试

在这里插入图片描述
其它功能正常


http://www.kler.cn/a/413346.html

相关文章:

  • Android 使用Charles抓包显示Unknown
  • Ubuntu20.04安装kalibr
  • PostgreSQL数据库的备份与恢复
  • PointNet++论文复现
  • python简单算法
  • 网站布局编辑器前端开发:设计要点与关键考量
  • 矩阵/矩阵乘法/特征值/特征向量的讲解
  • Docker 启动和停止的精准掌舵:操控指南
  • 学习JavaEE的日子 Day09 一维数组
  • 全景图像(Panorama Image)向透视图像(Perspective Image)的跨视图转化(Cross-view)
  • Paddle Inference部署推理(七)
  • QT 中 SQLite 使用方法
  • 第二节——计算机网络(四)物理层
  • Docker 容器隔离的关键技术:Namespace
  • PAT甲级 1056 Mice and Rice(25)
  • vue2 - 20.json-server
  • 优化DevOps环境中的容器化交付流程:实践指南
  • SpringBoot+Vue3+Element Plus实现图片上传和预览
  • k8s运行运行pod报错超出文件描述符表限制
  • BERT简单理解;双向编码器优势
  • Leetcode 131 Palindrome Partition
  • 使用 exe4j 将 Spring Boot 项目打包为 EXE 可执行文件
  • 前端面试笔试(六)
  • Ubuntu20.04安装kalibr
  • Linux: C语言解析域名
  • 探索光耦:光耦安全标准解读——确保设备隔离与安全的重要规范