From 3a711a466f83a61caa954e84fd5886508a6896ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E9=AB=98=E7=A7=91?= <18317545175@163.com> Date: Sun, 28 May 2023 21:56:06 +0800 Subject: [PATCH] zuoye --- .../zuoye04.md" | 265 ++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 "42 \345\210\230\351\253\230\347\247\221/zuoye04.md" diff --git "a/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" "b/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" new file mode 100644 index 0000000..73320e0 --- /dev/null +++ "b/42 \345\210\230\351\253\230\347\247\221/zuoye04.md" @@ -0,0 +1,265 @@ +```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 { + + } +} +``` + +```java +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; + } +} +``` + +```java +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 { + + } +} +``` + +```java +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(""); + } + } +} +``` + +```java +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%> | +修改 删除 | +