4月2日 JavaWeb 周一

时间:2022-04-25 22:10:50

网页列表增删改代码

展示的JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'show.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->

  </head>

  <body>
    <center>
        <h2>这是一个展示列表的页面</h2>
        <table bordercolor="red" border="2px">
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>性别</th>
            <th>年龄</th>
            <th>院系</th>
            <th>住址</th>
            <th><a href="/demo09_exam/add.jsp">增加</a>(<a href="/demo09_exam/upd.jsp">修改</a>)</th>
        </tr>
        <c:forEach var="li" items="${list00}">
            <tr>
                <td>
                    ${li.id}
                </td>
                <td>
                    ${li.name}
                </td>
                <td>
                    ${li.sex}
                </td>
                <td>
                    ${li.birth}
                </td>
                <td>
                    ${li.department}
                </td>
                <td>
                    ${li.address}
                </td>
                <td>
                    <a href="/demo09_exam/del?w=${li.id}" >删除</a>
                </td>
            </tr>
        </c:forEach>
        </table>
    </center>
  </body>
</html>

展示的Servlet代码

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mysql.jdbc.Driver;


public class ShowServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        List list = new ArrayList();
        try {
            //驱动
            Class.forName("com.mysql.jdbc.Driver");
            //连接
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","root");
            //sql语句
            String sql = "select * from t_user ;";
            //执行sql语句
            PreparedStatement pst = con.prepareStatement(sql);
            //结果集取值
            ResultSet rs = pst.executeQuery();

            while(rs.next()){
                String ids = rs.getString(1);
                String names = rs.getString(2);
                String sexs = rs.getString(3);
                String bis = rs.getString(4);
                String des = rs.getString(5);
                String ads = rs.getString(6);

                Elep el = new Elep(ids,names,sexs,bis,des,ads);
                list.add(el);
            }

            request.setAttribute("list00", list);
            request.getRequestDispatcher("show.jsp").forward(request, response);

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

增加的JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'add.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->

  </head>

  <body>
    <center>
        <form action="/demo09_exam/add" method="post">
            编号:<input type="text" name="bian"><br>
            姓名:<input type="text" name="xing"><br>
            性别:<input type="text" name="bie"><br>
            年龄:<input type="text" name="nian"><br>
            院系:<input type="text" name="yuan"><br>
            住址:<input type="text" name="zhu"><br>
            <input type="submit" value="提交">
        </form>
    </center>
  </body>
</html>

增加的Servlet代码

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;

import com.mysql.jdbc.Driver;


public class AddServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String bians = request.getParameter("bian");
        String xings = request.getParameter("xing");
        String bies = request.getParameter("bie");
        String nians = request.getParameter("nian");
        String yuans = request.getParameter("yuan");
        String zhus = request.getParameter("zhu");


        //JDBC
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","root");
            String sql = "insert into t_user values(?,?,?,?,?,?);";
            PreparedStatement pst = con.prepareStatement(sql);

            pst.setString(1, bians);
            pst.setString(2, xings);
            pst.setString(3, bies);
            pst.setString(4, nians);
            pst.setString(5, yuans);
            pst.setString(6, zhus);

            int x = pst.executeUpdate();

            if(x>0){
                response.sendRedirect("/demo09_exam/show");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

删除的Servlet代码

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;


public class DelServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String w = request.getParameter("w");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","root");
            String sql = "delete from t_user where id=?;";
            PreparedStatement pst = con.prepareStatement(sql);
            pst.setString(1, w);
            int x = pst.executeUpdate();

            if(x>0){
                response.sendRedirect("/demo09_exam/show");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}

修改的JSP页面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">

    <title>My JSP 'upd.jsp' starting page</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!-- <link rel="stylesheet" type="text/css" href="styles.css"> -->

  </head>

  <body>
        <center>
        <form action="/demo09_exam/update" method="post">
            编号:<input type="text" name="bian"><br>
            姓名:<input type="text" name="xing"><br>
            性别:<input type="text" name="bie"><br>
            年龄:<input type="text" name="nian"><br>
            院系:<input type="text" name="yuan"><br>
            住址:<input type="text" name="zhu"><br>
            <input type="submit" value="提交">
        </form>
    </center>
  </body>
</html>

修改的Servlet代码

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;


public class UpdateServlet extends HttpServlet {


    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //处理乱码
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        String bians = request.getParameter("bian");
        String xings = request.getParameter("xing");
        String bies = request.getParameter("bie");
        String nians = request.getParameter("nian");
        String yuans = request.getParameter("yuan");
        String zhus = request.getParameter("zhu");


        //JDBC
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/login","root","root");
            String sql = "update t_user set name=? , sex=? , birth=? , department= ? , address=? where id = ?;";
            PreparedStatement pst = con.prepareStatement(sql);

            pst.setString(1, xings);
            pst.setString(2, bies);
            pst.setString(3, nians);
            pst.setString(4, yuans);
            pst.setString(5, zhus);
            pst.setString(6, bians);

            int x = pst.executeUpdate();

            if(x>0){
                response.sendRedirect("/demo09_exam/show");
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);

    }

}

为集合创建的对象


public class Elep {
    String id;
    String name;
    String sex;
    String birth;
    String department;
    String address;


    public Elep(String id, String name, String sex, String birth, String department,
            String address) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.birth = birth;
        this.department = department;
        this.address = address;
    }


    public String getId() {
        return id;
    }


    public void setId(String id) {
        this.id = id;
    }


    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 String getBirth() {
        return birth;
    }


    public void setBirth(String birth) {
        this.birth = birth;
    }


    public String getDepartment() {
        return department;
    }


    public void setDepartment(String department) {
        this.department = department;
    }


    public String getAddress() {
        return address;
    }


    public void setAddress(String address) {
        this.address = address;
    }




}

运行项目需要配置好数据库,还需要导入三个jar包。

痛苦来自于对当下的批判:不能心平气和的接受当下,便是制造痛苦的根源。
——埃克哈特·托利 《当下的力量》