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="width: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:倉庫點此

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章