From 76f707bf915c885841ebfea3a63d1e7fc1f9ec98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=83=A0=E5=BF=A0?= <210372954@qq.com> Date: Sun, 28 May 2023 22:41:18 +0800 Subject: [PATCH] =?UTF-8?q?jsp=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230527 jsp\344\275\234\344\270\232.md" | 255 ++++++++++++++++++ 1 file changed, 255 insertions(+) create mode 100644 "25 \346\234\261\346\203\240\345\277\240/20230527 jsp\344\275\234\344\270\232.md" diff --git "a/25 \346\234\261\346\203\240\345\277\240/20230527 jsp\344\275\234\344\270\232.md" "b/25 \346\234\261\346\203\240\345\277\240/20230527 jsp\344\275\234\344\270\232.md" new file mode 100644 index 0000000..687d99c --- /dev/null +++ "b/25 \346\234\261\346\203\240\345\277\240/20230527 jsp\344\275\234\344\270\232.md" @@ -0,0 +1,255 @@ +```java +package sql; + +import jakarta.servlet.*; +import jakarta.servlet.http.*; +import jakarta.servlet.annotation.*; +import tools.Jdbc; + +import java.io.IOException; + +@WebServlet(name = "Delete", value = "/Delete") +public class Delete extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String id = request.getParameter("id"); + System.out.println(id); + int i = Jdbc.update("delete from student where id=?", id); + if (i>0){ + response.getWriter().write(""); + }else { + response.getWriter().write(""); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +package sql; + +public class Student { + private String id; + private String name; + + public Student() { + } + + public Student(String id, String name) { + this.id = id; + this.name = name; + } + + 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; + } +} + +package sql; + +import jakarta.servlet.*; +import jakarta.servlet.http.*; +import jakarta.servlet.annotation.*; +import tools.Jdbc; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet(name = "Update", value = "/Update") +public class Update extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String id = request.getParameter("id"); + try { + ResultSet re = Jdbc.select("select * from student where id=?", id); + re.next(); + Student s = new Student(re.getString(1), re.getString(2)); + request.setAttribute("student",s); + request.getRequestDispatcher("/update.jsp").forward(request,response); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +package sql; + +import jakarta.servlet.*; +import jakarta.servlet.http.*; +import jakarta.servlet.annotation.*; +import tools.Jdbc; + +import java.io.IOException; + +@WebServlet(name = "Update2", value = "/Update2") +public class Update2 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"); + int i = Jdbc.update("update student set name=? where id=?", name, id); + if (i > 0) { + response.getWriter().write(""); + } else { + response.getWriter().write(""); + } + } +} + +package tools; + +import java.sql.*; + +public class Jdbc { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + //创建连接 + private static Connection conn() { + String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEncoding=utf8"; + Connection conn = null; + try { + conn = DriverManager.getConnection(url, "root", "sxxddytdn"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + + //查询 + public static ResultSet select(String sql, String... arr) { + ResultSet re = null; + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i + 1), arr[i]); + } + re = pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return re; + } + + //增删改 + public static int update(String sql, String... arr) { + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i + 1), arr[i]); + } + int i = pst.executeUpdate(); + return i; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` + +```jsp +<%@ page import="java.sql.ResultSet" %> +<%@ page import="tools.Jdbc" %><%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/5/19 + Time: 17:03 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + +
+编号 | +姓名 | +操作 | +
---|---|---|
<%=id%> | +<%=name%> | +修改 删除 | +