diff --git "a/41 \347\224\230\345\260\217\345\274\272/20230528.md" "b/41 \347\224\230\345\260\217\345\274\272/20230528.md" new file mode 100644 index 0000000000000000000000000000000000000000..949e244d281be69aa69388c681d55e57b25073da --- /dev/null +++ "b/41 \347\224\230\345\260\217\345\274\272/20230528.md" @@ -0,0 +1,278 @@ +```java +package javabean; + +public class Student { + private int id; + private String name; + private String sex; + + public Student() { + } + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } + + public int getId() { + return id; + } + + public void setId(int 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; + } +} + +``` + +```java +package servlet; + +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet(name = "deleteById", value = "/deleteById") +public class deleteById extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html;charset=utf-8"); + request.setCharacterEncoding("utf-8"); + String id = request.getParameter("id"); + String sql="delete from stu where id=?"; + try { + int i = DBUtil.update(sql,id); + if (i>0){ + response.getWriter().write(""); + }else { + response.getWriter().write(""); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + + + +```java +package servlet; + +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet(name = "savaUpdate", value = "/savaUpdate") +public class savaUpdate extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doPost(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String id = request.getParameter("id"); + String name = request.getParameter("name"); + String sex = request.getParameter("sex"); + + String sql="update stu set name=?,sex=? where id=?"; + + int i = 0; + try { + i = DBUtil.update(sql,name,sex,id); + } catch (SQLException e) { + throw new RuntimeException(e); + } + if (i>0){ + response.getWriter().write(""); + }else { + response.getWriter().write(""); + + } + + + + } +} + +``` + +```java +package servlet; + +import javabean.Student; +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet(name = "updateById", value = "/updateById") +public class updateById extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html;charset=utf-8"); + request.setCharacterEncoding("utf-8"); + String id = request.getParameter("id"); + String sql="select * from stu where id=?"; + try { + ResultSet rs = DBUtil.query(sql,id); + rs.next(); + int id1 = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + Student stuu = new Student(id1, name, sex); + // 将stu对象,添加到request.并转发给显示的网页 + request.setAttribute("student",stuu); + request.getRequestDispatcher("/edit.jsp").forward(request,response); + + + } catch (SQLException e) { + throw new RuntimeException(e); + } + + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```jsp +<%@ page import="javabean.Student" %><%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-05-27 + Time: 17:11 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改信息 + + +<% + Student student =(Student) request.getAttribute("student"); +%> + + + +
+ + 姓名:
+ 性别:
+
+ + +
+ + + + + + +``` + +```jsp +<%@ page import="utils.DBUtil" %> +<%@ page import="java.sql.ResultSet" %><%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-05-27 + Time: 15:31 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + $Title$ + + + + + + <% + String sql="select * from stu"; + ResultSet rs = DBUtil.query(sql); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + %> + + + + + + + + <% + } + %> + + + + + +
编号姓名性别操作
<%=id%><%=name%><%=sex%>修改 删除
+ + +``` +