Web的Java开发基础分享——学生信息管理系统(四)

写在前面:

因为这学期博主修的课太多了,所以一直没怎么更新。

很惊喜,因为之前发的学生信息管理系统多了很多人flow我,也有很多人通过QQ联系我,希望能帮忙解决一些问题,我也是尽力去帮忙。

前两天有位同学跟我说为什么没有修改功能,我自己看了一下,确实是没有,也答应他这两天要补充一下,所以有了这篇博客。

正文:

思路:在原来显示列表页面“添加学生”按钮旁添加一个“修改”按钮,

点击按钮跳转到修改页面,修改页面填写信息后,发送到后台servlet处理,

处理完成后,再转会显示列表页面。

1)添加“修改按钮”

源码如下:

            <input type="button" value="修改" 
                   onclick="javascrtpt:window.location.href = 'editStu.jsp'">

 2)编辑修改页面

这一步只需要在原来addStu.jsp页面上做一些简单的修改,我直接贴上截图和源码,可以自己对比下区别

源码:

<!--这里是pages文件夹里的addStu.jsp-->
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width,initial-scale=1.0, 
              minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
        <title>修改学生信息</title>
    </head>
    <body>
        <h2 align="center">请输入要修改学生信息</h2>
        <div style="100%;text-align:center">
            <form action="../editServlet">
                <table border=1 style="margin:auto">
                    <tr>
                        <td>学号:</td>
                        <td><input type="text" name="stuid" id="stuid"></td>
                    </tr>
                    <tr>
                        <td>姓名:</td>
                        <td><input type="text" name="stuname" id="stuname"></td>
                    </tr>
                    <tr>
                        <td>性别:</td>
                        <td><input type="text" name="stusex" id="stusex"></td>
                    </tr>
                    <tr>
                        <td>年龄:</td>
                        <td><input type="text" name="stuage" id="stuage"></td>
                    </tr>
                    <tr>
                        <td>年级:</td>
                        <td><input type="text" name="stugrade" id="stugrade"></td>
                    </tr>
                    <tr>
                        <td>个人简介:</td>
                        <td><input type="text" name="stuintroduce" id="stuintroduce"></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <input type="submit" value="提交">
                        </td>
                    </tr>
                </table>
            </form>
        </div>
    </body>
</html>

3)在后台的editServlet页面做处理(和数据库交互)

这一步依然是拷贝addServlet的代码,做一些简单的修改  比如说SQL语句 即可

源码如下:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package p1;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Administrator
 */
public class editServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        try (PrintWriter out = response.getWriter()) {
            
            String driver="com.mysql.jdbc.Driver";
            String url="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
            String usr="root";
            String password="root";
            try{
                Class.forName(driver);
                Connection conn=DriverManager.getConnection(url,usr,password);
                String sqlString="UPDATE stulist SET name = ?, sex = ?, age = ?, grade = ?, introduce = ? WHERE id = ?";
                
                PreparedStatement pstmt=conn.prepareStatement(sqlString);
                
                int id = Integer.parseInt(request.getParameter("stuid"));
                String name = request.getParameter("stuname");
                String sex = request.getParameter("stusex");
                int age = Integer.parseInt(request.getParameter("stuage"));
                String grade = request.getParameter("stugrade");
                String introduce = request.getParameter("stuintroduce");
                
                pstmt.setString(1, name);
                pstmt.setString(2, sex);
                pstmt.setInt(3, age);
                pstmt.setString(4, grade);
                pstmt.setString(5, introduce);     
                pstmt.setInt(6, id);
                
                pstmt.executeUpdate();
            }catch(Exception e){ 
                System.err.println("error:"+e);
            }
            response.sendRedirect("pages/displayStuList.jsp");
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

提供Demo地址:点此查看Demo

项目源码已提交至Github:仓库点此

原文地址:https://www.cnblogs.com/kareza/p/13393438.html