From 97e1d197dd9b694b1c9d1439707d78c639136362 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=B1=BD=E5=A2=83?= <1537282342@qq.com> Date: Sun, 28 May 2023 22:56:54 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...7\346\234\254\346\226\207\346\241\243.txt" | 252 ++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 "28 \351\273\204\346\242\223\345\242\203/29239528/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" diff --git "a/28 \351\273\204\346\242\223\345\242\203/29239528/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" "b/28 \351\273\204\346\242\223\345\242\203/29239528/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" new file mode 100644 index 0000000..21243fa --- /dev/null +++ "b/28 \351\273\204\346\242\223\345\242\203/29239528/\346\226\260\345\273\272\346\226\207\346\234\254\346\226\207\346\241\243.txt" @@ -0,0 +1,252 @@ +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%> | +修改 删除 | +