From c1c6c08be943770ad9d684d9ff52bb4eabea19ec Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 27 May 2023 12:25:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230524\344\275\234\344\270\232.txt" | 245 ++++++++++++++++++ 1 file changed, 245 insertions(+) create mode 100644 "43 \345\272\236\346\200\235\350\277\234/20230524\344\275\234\344\270\232.txt" diff --git "a/43 \345\272\236\346\200\235\350\277\234/20230524\344\275\234\344\270\232.txt" "b/43 \345\272\236\346\200\235\350\277\234/20230524\344\275\234\344\270\232.txt" new file mode 100644 index 0000000..3ba5a43 --- /dev/null +++ "b/43 \345\272\236\346\200\235\350\277\234/20230524\344\275\234\344\270\232.txt" @@ -0,0 +1,245 @@ +!DOCTYPE html +html lang=en +head + meta charset=UTF-8 + title测试title +head +body + h1添加学生h1 + form action=.add method=post + 学号 input type=text name=idbr + 姓名 input type=text name=namebr + 性别 input type=text name=sexbr + input type=submit value=添加 + form + + h1修改学生信息h1 + form action=.modify method=post + 请输入要修改学生的学号 input type=text name=idbr + 姓名 input type=text name=namebr +!-- 性别 input type=text name=sexbr-- + input type=submit value=修改 + form + + h1删除学生h1 + form action=.delete method=post + 请输入需要删除学生的学号br input type=text name=id br + input type=submit value=删除 + form + + h1查找学生h1 + form action=.seek method=post + 请输入需要查询的学生编号 input type=text name=id + input type=submit value=查找 + form +body +html +``` + +```java +public class DbJbdc { + private static final String url=jdbcmysqlstudent_dbuseSSL=false&useUnicode=true&characterEncoding=utf8; + private static final String user=root; + private static final String pass=200311; + public static Connection link() throws ClassNotFoundException, SQLException { + Class.forName(com.mysql.cj.jdbc.Driver); + return DriverManager.getConnection(url, user, pass); + } + public static int currencyUpdate(String sql,String ...flop) throws SQLException, ClassNotFoundException { + Connection conn = link(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i flop.length; i++) { + prs.setString((i+1),flop[i]); + } + int i = prs.executeUpdate(); + return i; + } + public static int delete(String sql,String ...flop) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, flop); + return i; + } + public static int modify(String sql,String ...flop) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, flop); + return i; + } + public static int add(String sql,String ...flop) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, flop); + return i; + } + public static ResultSet Query(String sql,String ...flop) throws SQLException, ClassNotFoundException { + Connection conn = link(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i flop.length; i++) { + prs.setString((i+1),flop[i]); + } + ResultSet rs = prs.executeQuery(); + return rs; + } +} +``` + +```java + +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet(seek) +public class seek extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType(texthtml;charset=utf-8); + resp.setCharacterEncoding(utf-8); + try { + String sql=select from student; + ResultSet rs = DbJbdc.Query(sql); + while (rs.next()){ + int id = rs.getInt(id); + String name = rs.getString(name); + String sex = rs.getString(sex); + resp.getWriter().write(编号+id+姓名+name+性别+sex+br); + } + } catch (SQLException ClassNotFoundException e) { + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setCharacterEncoding(utf-8); + resp.setContentType(texthtml;charset=utf-8); + try { + String idbh = req.getParameter(id); + ResultSet rs = DbJbdc.Query(select from student where id=, idbh); + while (rs.next()){ + int id = rs.getInt(id); + String name = rs.getString(name); + String sex = rs.getString(sex); + resp.getWriter().write(编号+id+姓名+name+性别+sex+br); + } + } catch (SQLException ClassNotFoundException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java + +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.sql.SQLException; +@WebServlet(add) +public class add 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 { + resp.setContentType(texthtml;charset=utf-8); + resp.setCharacterEncoding(utf-8); + String idbh = req.getParameter(id); + String namebh = req.getParameter(name); + String sexbh = req.getParameter(sex); + try { + int add = DbJbdc.add(insert into student values (,,);, idbh, namebh, sexbh); + if (add0){ + resp.getWriter().write(成功); + }else { + resp.getWriter().write(失败); + } + } catch (SQLException ClassNotFoundException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet(delete) +public class delete 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 { + resp.setContentType(texthtml;charset=utf-8); + resp.setCharacterEncoding(utf-8); + String idbh = req.getParameter(id); + try { + int delete = DbJbdc.delete(delete from student where id=, idbh); + if (delete0){ + resp.getWriter().write(成功); + }else { + resp.getWriter().write(失败); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.sql.SQLException; +@WebServlet(modify) +public class modify 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 { + resp.setContentType(texthtml;charset=utf-8); + resp.setCharacterEncoding(utf-8); + String idbh = req.getParameter(id); + String namebh = req.getParameter(name); + String sexbh = req.getParameter(sex); + try { + int modify = DbJbdc.modify(update student set name= where id=, namebh,idbh); + if (modify0){ + resp.getWriter().write(成功); + }else { + resp.getWriter().write(失败); + } + } catch (SQLException ClassNotFoundException e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file -- Gitee From 0f67ab528467c0a86f9b314391fa9e3f7df63bf0 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 28 May 2023 23:52:31 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BD=9C=E6=A5=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230527\344\275\234\346\245\255.txt" | 270 ++++++++++++++++++ 1 file changed, 270 insertions(+) create mode 100644 "43 \345\272\236\346\200\235\350\277\234/20230527\344\275\234\346\245\255.txt" diff --git "a/43 \345\272\236\346\200\235\350\277\234/20230527\344\275\234\346\245\255.txt" "b/43 \345\272\236\346\200\235\350\277\234/20230527\344\275\234\346\245\255.txt" new file mode 100644 index 0000000..f058d75 --- /dev/null +++ "b/43 \345\272\236\346\200\235\350\277\234/20230527\344\275\234\346\245\255.txt" @@ -0,0 +1,270 @@ +~~~ 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" %> + + + 学生列表 + + + + + + + + + + + <% + ResultSet re = Jdbc.select("select * from student"); + while (re.next()) { + String id = re.getString(1); + String name = re.getString(2); + %> + + + + + + <% + } + %> +
编号姓名操作
<%=id%><%=name%>修改 删除
+ + + +``` + +```jsp +<%@ page import="sql.Student" %><%-- + Created by IntelliJ IDEA. + User: NO.1 + Date: 2023/5/28 + Time: 20:29 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改 + + +<% + Student s = (Student) request.getAttribute("student"); +%> +
+
+ 姓名:
+ +
+ + +~~~ + -- Gitee