diff --git "a/23 \351\273\204\346\265\251\344\270\234/20230528 \347\275\221\351\241\265\345\210\240\351\231\244\344\277\256\346\224\271\344\275\234\344\270\232.md" "b/23 \351\273\204\346\265\251\344\270\234/20230528 \347\275\221\351\241\265\345\210\240\351\231\244\344\277\256\346\224\271\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..8fc73d1af26b94215c6b11daf78ef2b6b7bf38ea --- /dev/null +++ "b/23 \351\273\204\346\265\251\344\270\234/20230528 \347\275\221\351\241\265\345\210\240\351\231\244\344\277\256\346\224\271\344\275\234\344\270\232.md" @@ -0,0 +1,262 @@ +```java +// 初始网页 +<%@ page import="javax.xml.transform.Result" %> +<%@ page import="java.sql.ResultSet" %> +<%@ page import="test.Tool" %><%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-05-27 + Time: 15:29 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生列表 + + + + + + + + + + <% + String url = "select * from student"; + ResultSet res = Tool.select(url); + while (res.next()){ + int id = res.getInt("id"); + String name = res.getString("name"); + String sex = res.getString("sex"); + %> + + + + + + + <% + } + %> +
+ 学号 + + 姓名 + + 性别 + + 操作 +
+ <%=id%> + <%=name%><%=sex%>删除 修改
+ + + +``` + +```Java +// 删除 +package test; + +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 Servlet extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); + String id = req.getParameter("id"); + String sql = "delete from student where id = ?"; + try { + int i = Tool.DDL(sql,id); + if (i>0){ + System.out.println("删除成功"); + resp.getWriter().write(""); + } + else { + System.out.println("删除失败"); + resp.getWriter().write("删除失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```java +//修改数据 +package test; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet(name = "update" ,value = "/updateById") +public class ServletUp extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); + try { + //获取信息 + String id = req.getParameter("id"); + String select = "select * from student where id = ?"; + ResultSet res = Tool.select(select,id); + res.next(); + String oldID = res.getString("id"); + String oldName= res.getString("name"); + String oldSex = res.getString("sex"); + Student stu = new Student(oldID,oldName,oldSex); + req.setAttribute("student",stu); + req.getRequestDispatcher("/Update.jsp").forward(req,resp); + } + catch (SQLException e){ + e.printStackTrace(); + } + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} + +``` + +```jsp +//修改网页 +<%@ page import="test.Student" %><%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-05-28 + Time: 19:28 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改 + + + <%Student student = (Student)request.getAttribute("student"); + %> +

<%student.getId();%>

+
+
+ 姓名:
+ 性别:
+ +
+ + + +``` + +```java +// 修改运行 +package test; + +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet(name = "ServletUpdate",value = "/ServletUpdate") +public class ServletUpdate extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); + String id = req.getParameter("id"); + String name = req.getParameter("name"); + String sex = req.getParameter("sex"); + String sql = "update student set name = ?,sex = ? where id = ?"; + try { + int i = Tool.DDL(sql,name,sex,id); + if (i>0){ + System.out.println("修改成功"); + resp.getWriter().write(""); + } + else { + System.out.println("修改失败"); + resp.getWriter().write("修改失败"); + } + + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java +// 学生类 +package test; + +public class Student { + String id; + String name; + String sex; + + 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 Student(String id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } +} + +``` +