From 406c1c0d3c7b6b68ab6ac96145cbedc0cb519794 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E7=8E=AE=E9=94=8B?= <295357613@qq.com> Date: Tue, 23 May 2023 22:42:10 +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 --- .../20230522 Tomcat\344\270\216servlet.md" | 221 ++++++++++++++++++ 1 file changed, 221 insertions(+) create mode 100644 "17\351\203\221\347\216\256\351\224\213/20230522 Tomcat\344\270\216servlet.md" diff --git "a/17\351\203\221\347\216\256\351\224\213/20230522 Tomcat\344\270\216servlet.md" "b/17\351\203\221\347\216\256\351\224\213/20230522 Tomcat\344\270\216servlet.md" new file mode 100644 index 0000000..9e657ba --- /dev/null +++ "b/17\351\203\221\347\216\256\351\224\213/20230522 Tomcat\344\270\216servlet.md" @@ -0,0 +1,221 @@ +## 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(2) +); +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.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.*; + +//查询student +@WebServlet("/dmsy") +public class Test01 extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "select * from student"; + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf8"); + try { + ResultSet rs = DBUtil.query(sql); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + System.out.println(id+name+sex); + resp.getWriter().write(id+"\t"+name+"\t"+sex+"
"); + } + rs.close(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + + //添加 + @WebServlet("/add") + public class Add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "insert into student values (null,'?','?')"; + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf8"); + try { + int i = DBUtil.update(sql,"宋扬","男"); + if(i>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } + } + //修改 + @WebServlet("/update") +public class Update extends HttpServlet{ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "update student set sex='男' where id=?"; + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf8"); + try { + int i = DBUtil.update(sql, "1"); + if(i>0){ + System.out.println("成功"); + resp.getWriter().write("成功"); + }else { + System.out.println("失败"); + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + + + } + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +//删除 + @WebServlet("/delete") +public class Delete extends HttpServlet{ + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String sql = "delete from student where id=?"; + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf8"); + try { + int i = DBUtil.update(sql, "2"); + if(i>0){ + System.out.println("成功"); + resp.getWriter().write("成功"); + }else { + System.out.println("失败"); + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +} + +~~~ + +修改 + +~~~java +、、、 +~~~ + +删除 + +~~~java +、、、 +~~~ + +增加 + +~~~java +、、、 +~~~ + -- Gitee