From 23caf473cfa84d9d4a9ae8e980f688985468b144 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E9=BB=84=E6=B5=A9=E4=B8=9C?= <3187628460@qq.com>
Date: Sun, 28 May 2023 21:11:39 +0800
Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E9=A1=B5=E5=88=A0=E9=99=A4,=E4=BF=AE?=
=?UTF-8?q?=E6=94=B9=E4=BD=9C=E4=B8=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...56\346\224\271\344\275\234\344\270\232.md" | 262 ++++++++++++++++++
1 file changed, 262 insertions(+)
create mode 100644 "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"
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 0000000..8fc73d1
--- /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;
+ }
+}
+
+```
+
--
Gitee