From 6a96cc188df6fe4a3894b390acb17132dd5b6823 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=AE=8B=E5=AE=8F=E6=89=AC?= Date: Fri, 26 May 2023 23:04:15 +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 --- ...23\347\232\204\344\275\234\344\270\232.md" | 283 ++++++++++++++++++ 1 file changed, 283 insertions(+) create mode 100644 "14 \345\256\213\345\256\217\346\211\254/20230524\345\205\263\344\272\216HTTP\346\223\215\344\275\234\346\225\260\346\215\256\345\272\223\347\232\204\344\275\234\344\270\232.md" diff --git "a/14 \345\256\213\345\256\217\346\211\254/20230524\345\205\263\344\272\216HTTP\346\223\215\344\275\234\346\225\260\346\215\256\345\272\223\347\232\204\344\275\234\344\270\232.md" "b/14 \345\256\213\345\256\217\346\211\254/20230524\345\205\263\344\272\216HTTP\346\223\215\344\275\234\346\225\260\346\215\256\345\272\223\347\232\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000..b4b3a2f --- /dev/null +++ "b/14 \345\256\213\345\256\217\346\211\254/20230524\345\205\263\344\272\216HTTP\346\223\215\344\275\234\346\225\260\346\215\256\345\272\223\347\232\204\344\275\234\344\270\232.md" @@ -0,0 +1,283 @@ +## MySQL + +~~~java +create database student_db character set utf8; +use student_db; +create table student( + id int primary key auto_increment, + name varchar(20), + sex varchar(20) +); +insert into student values +(null,'蔡泽钦','女'), +(null,'菜则亲','男'), +(null,'菜择沁','女'); +~~~ + +## DButil + +~~~java +package com.sonyang.servlet; + +import java.sql.*; + +public class DBUtil { + private static final String url= "jdbc:mysql://localhost:3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String password="root"; + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + + } + public static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url,username,password); + return conn; + } + //查询 + public static ResultSet query(String sql,String ...keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length ; i++) { + pst.setString((i+1),keys[i]); + } + ResultSet rs = pst.executeQuery(); + return rs; + } + //更新 + public static int update(String sql,String ...keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + for (int i = 0; i < keys.length ; i++) { + pst.setString((i+1),keys[i]); + } + int i = pst.executeUpdate(); + return i; + + } + //释放资源 + public static void close(Connection conn,PreparedStatement pst,ResultSet rs) throws SQLException { + if (rs!=null){ + rs.close(); + } + if (pst!=null){ + pst.close(); + } + if (conn!=null){ + conn.close(); + } + } +} + +~~~ + +## 测试类 + +查询 + +~~~java +package com.sonyang.servlet; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet("/select") +public class Select extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String sql = "select * from student where id=?"; + String id = request.getParameter("id"); + try { + ResultSet rs = DBUtil.query(sql,id); + while (rs.next()){ + int id1 = rs.getInt("id"); + String name2 = rs.getString("name"); + String sex3 = rs.getString("sex"); + response.getWriter().write(id1+"\t"+name2+"\t"+sex3+"
"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +~~~ + +修改 + +~~~java +package com.sonyang.servlet; + +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("/update") +public class Update extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf8"); + resp.setContentType("text/html;charset=utf-8"); + String sql = "update student set name=?,sex=? where id=?"; + String id = req.getParameter("id"); + String name = req.getParameter("name"); + String sex = req.getParameter("sex"); + try { + int i = DBUtil.update(sql, name, sex,id); + if (i > 0) { + resp.getWriter().write("修改成功"); + } else { + resp.getWriter().write("修改失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} +~~~ + +删除 + +~~~java +package com.sonyang.servlet; + +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("/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 { + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); + String sql = "delete from student where id=?"; + String id = req.getParameter("id"); + try { + int i = DBUtil.update(sql, id); + if(i>0){ + resp.getWriter().write("删除成功"); + }else { + resp.getWriter().write("删除失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +~~~ + +增加 + +~~~java +package com.sonyang.servlet; + +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("/add") +public class Add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); + String sql = "insert into student values (?,?,?)"; + String id = req.getParameter("id"); + String name = req.getParameter("name"); + String sex = req.getParameter("sex"); + try { + int i = DBUtil.update(sql,id,name,sex); + if(i>0){ + resp.getWriter().write("添加成功"); + }else { + resp.getWriter().write("添加失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +~~~ + +HTML + +~~~jsp + + + + + 学生管理系统 + + +

查询学生信息

+
+ 查询的学生编号
+ +
+

添加学生信息

+
+ 添加的学生编号
+ 添加的学生名字
+ 添加的学生性别
+ +
+

删除学生信息

+
+ 删除的学生编号
+ +
+

输入学生编号修改信息

+
+ 修改的学生编号
+ 修改后的学生名字
+ 修改后的学生性别
+ +
+ + +~~~ + -- Gitee