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

2-jsp-实现增删改功能

jsp改造stuid项目

将所有的前端页面改为.jsp

image-20241202161941869

  • 确定编码格式

image-20241202162055015

将所有的项目名改为jsp动态获取

image-20241202162205042

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>

    <a href="<%=request.getContextPath() + "/list"%>">启动学生管理系统</a>
</body>
</html>

image-20241202162621825

image-20241202162627306

  • 成功访问,==》 该项目成功

新增功能

image-20241202163050782

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>添加学生</h1>
<%--    动态获取项目名字--%>
    <form action="<%=request.getContextPath() + "/add"%>">
        姓名:<input type="text" name="name">
        <br>
        学号:<input type="text" name="stuid">
        <br>
        专业:<input type="text" name="zhuanye" >
        <br><input type="submit" value="确认提交">

    </form>
    
</body>
</html>

image-20241202163315584

doAdd( )

 private void doAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        需要获取请求数据,所以,这里必须设置编码,跟查询不一样
        request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String stuid = request.getParameter("stuid");
        String zhuanye = request.getParameter("zhuanye");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);
            String sql = "INSERT INTO student(name,stuid,zhuanye) VALUES(?,?,?)";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,stuid);
            preparedStatement.setString(3,zhuanye);
            preparedStatement.execute();



            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(resultSet,preparedStatement,connection);
        }
//        进行重定向
        response.sendRedirect(request.getContextPath()+"/list");

    }

image-20241202164148159

修改功能

修改前的查询功能

<%@ page import="java.util.ArrayList" %>
<%@ page import="com.bean.Student" %>
<%@ page import="java.util.List" %><%--设置类型与编码格式--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>学生信息</h1>
    <table border="1px" width="600px">
        <tr>
            <td>姓名</td>
            <td>学号</td>
            <td>专业</td>
            <td>操作</td>
        </tr>
<%--        获取请求域中拿到  集合--%>
<%--        使用 多个 " <%  %>"--%>
        <%

            List<Student> studentList = (List<Student>)request.getAttribute("studentList");
            for (Student student : studentList){





        %>
        <tr>
<%--            <%= 含有变量的字符串/变量  %>--%>
<%--            通过集合中每一个Student 对象调研 bean  对应的方法来获取 name  stuid zhuanye--%>
            <td><%=student.getName()%></td>
            <td><%=student.getStuid()%></td>
            <td><%=student.getZhuanye()%></td>
            <td>
                <a href="add.jsp">新增</a>
<%--                点击修改并发送学号--%>
                <a href="<%=request.getContextPath() + "/modifyQuery" + "?stuno=" + student.getStuid()%>">修改</a>
                <a href="#">删除</a>
            </td>
        </tr>

        <%
            }
        %>

    </table>
</body>
</html>

doModifyQuery()

private void doModifyQuery(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        request.setCharacterEncoding("UTF-8");
        String stuno = request.getParameter("stuno");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
//        放大 list 集合的作用域,存放每一个student 对象
        List<Student> students = new ArrayList<>();
        try {
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);
            String sql = "SELECT * FROM student where STUID = '"+stuno+"'";
            preparedStatement = connection.prepareStatement(sql);
            resultSet  = preparedStatement.executeQuery();
//            处理结果集,并封装到 集合中
            while (resultSet.next()){
//                获取表中的字段
                String name = resultSet.getString("name");
                String stuid = resultSet.getString("stuid");
                String zhuanye = resultSet.getString("zhuanye");
//                将字段封装到 bean
                Student student = new Student();
                student.setName(name);
                student.setStuid(stuid);
                student.setZhuanye(zhuanye);
//              将每一个对象添加到list集合
                students.add(student);

            }

            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(resultSet,preparedStatement,connection);
        }
//        数据添加到请求域
        request.setAttribute("studentList",students);
//        转发
        request.getRequestDispatcher("/modify.jsp").forward(request,response);

    }

修改前的展示与修改数据的提交

<%@ page import="java.util.List" %>
<%@ page import="com.bean.Student" %>
<%@ page import="java.util.Iterator" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h1>修改学生</h1>
    <form action="<%=request.getContextPath() + "/modify"%>">
        <%
            List<Student> studentList = (List<Student>) request.getAttribute("studentList");
//            集合中只有一个学生类对象,不需要循环
            Iterator<Student> iterator = studentList.iterator();
            Student student= iterator.next();

        %>
        原姓名:<%=student.getName()%>
        <br>
        原学号:<%=student.getStuid()%>
        <br>
        原专业:<%=student.getZhuanye()%>
        <br>
        姓名:<input type="text" name="name">
        <br>
        学号:<input type="text" name="stuid"  value="<%=student.getStuid()%>" readonly>
        <br>
        专业:<input type="text" name="zhuanye" >
        <br><input type="submit" value="确认修改">

    </form>
</body>
</html>

处理修改提交的数据

doModify()

private void doModify(HttpServletRequest request, HttpServletResponse response) throws IOException {
//        需要获取输入的数据,所以,编码格式
        request.setCharacterEncoding("utf-8");
        String stuno = request.getParameter("stuid");
        String name = request.getParameter("name");
        String zhuanye = request.getParameter("zhuanye");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);
            String sql = "update student set name =?,zhuanye=? where stuid =?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,name);
            preparedStatement.setString(2,zhuanye);
            preparedStatement.setString(3,stuno);
            preparedStatement.execute();



            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(resultSet,preparedStatement,connection);
        }
//        进行重定向
        response.sendRedirect(request.getContextPath()+"/list");

    }

image-20241202224529376

删除功能

image-20241202225810342

private void doDel(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        String stuno = request.getParameter("stuno");
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = DBUtil.getConnection();
            connection.setAutoCommit(false);
            String sql = "delete from student where stuid =?";
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1, stuno);
            preparedStatement.execute();


            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(resultSet, preparedStatement, connection);
        }
//        进行重定向
        response.sendRedirect(request.getContextPath() + "/list");

    }

image-20241202225309764


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

相关文章:

  • Git Rebase vs Merge:操作实例详解
  • java将word docx pdf转换为图片(不需要额外下载压缩包,直接导入maven坐标)
  • 使用Python和OpenCV自动检测并去除图像中的字幕
  • 【第 1 章 初识 C 语言】1.8 使用 C 语言的 7 个步骤
  • golangclean-cache, modcache sumdb等配置
  • vue3+typescript自定义input组件
  • 【从0学英语】形容词性/名词性物主代词是什么?
  • 深入理解计算机系统,源码到可执行文件翻译过程:预处理、编译,汇编和链接
  • 一.准备环境,从零开始搭建项目
  • Hive学习基本概念
  • Java 中 ArrayList 与 LinkedList 的详细比较
  • 什么是 KDE?
  • numpy.float8不存在;Python中,实现16位浮点数
  • 种花问题算法
  • 运维工作常用Shell脚本(Commonly Used Shell Scripts for Operation and Maintenance Work)
  • 深入解析 Python 异步编程中的 `gather`、`as_completed` 和 `wait`
  • SQL注入--基本概念
  • 01-标准库开发-STM32定时器
  • 为什么在服务器上设置 fish 为默认 shell, vscode remote ssh 默认还是 bash?
  • flink学习(13)—— 重试机制和维表join
  • 在 uniapp 项目中使用 Iconify 字体图标库
  • 《Python PDF 格式转换全攻略》
  • Linux 进程管理详解
  • 张量并行和流水线并行在Transformer中的具体部位
  • 25.4K Star 高效内存数据存储!特别好用的Redis 和 Memcached 替代品:Dragonfly!
  • redisson-spring-data与Spring-Data-Redis的版本关系问题