From 7821c506e9e0d3e8463a92e89f4673e5f8c58b35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=AC=A3?= <3188923799@qq.com> Date: Tue, 23 May 2023 22:01:16 +0800 Subject: [PATCH] =?UTF-8?q?tomcat=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...20230522tomcat\344\275\234\344\270\232.md" | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) create mode 100644 "29 \347\216\213\351\233\257\351\235\231/20230522tomcat\344\275\234\344\270\232.md" diff --git "a/29 \347\216\213\351\233\257\351\235\231/20230522tomcat\344\275\234\344\270\232.md" "b/29 \347\216\213\351\233\257\351\235\231/20230522tomcat\344\275\234\344\270\232.md" new file mode 100644 index 0000000..f94b2ee --- /dev/null +++ "b/29 \347\216\213\351\233\257\351\235\231/20230522tomcat\344\275\234\344\270\232.md" @@ -0,0 +1,174 @@ +```Java +# 作业, + * 1 数据库相关的操作,使用封装的工具类 + * 2 编写四个servlet,使用注解指定相关的访问路径,分别对应查询,修改,删除,添加的操作 + * 3 从浏览器中,访问这中个路径,显示响应的信息,查询显示结果,其它的显示成功或是失败 + * 4 预习题:如何通过浏览器传送请求参数给servlet,servlet如何接收这些参数,并使用这些参数,去影响数据库的操作? +``` + +```java + +package com.qiu.student; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class DBUtil { + private static final String url = "jdbc:mysql:///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) { + System.out.println("驱动注册异常"); + } + } + public static Connection getConn(){ + Connection conn = null; + try { + conn = DriverManager.getConnection(url, username, password); + } catch (SQLException e) { + System.out.println("数据库连接异常"); + } + return conn; + } +} +package com.qiu.student; + +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.io.PrintWriter; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet("/select") +public class TestTomcat extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + PrintWriter out = resp.getWriter(); + Connection conn = DBUtil.getConn(); + try { + PreparedStatement pst = conn.prepareStatement("select * from student"); + ResultSet rs = pst.executeQuery(); + out.write(""); + out.write(""); + while (rs.next()){ + out.write(""); + out.write(""); + out.write(""); + } + out.write("
学号姓名性别
"+rs.getInt("stuId")+""+rs.getString("name")+""+rs.getString("sex")+"
"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +package com.qiu.student; + +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.io.PrintWriter; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +@WebServlet("/insert") +public class Insert extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + Connection conn = DBUtil.getConn(); + try { + PreparedStatement pst = conn.prepareStatement("insert into student values (329,'小王','女')"); + int i = pst.executeUpdate(); + resp.setContentType("text/html;charset=utf-8"); + PrintWriter out = resp.getWriter(); + if (i>0){ + out.write("添加成功"); + }else { + out.write("添加失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +package com.qiu.student; + +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.io.PrintWriter; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +@WebServlet("/update") +public class Set extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + Connection conn = DBUtil.getConn(); + try { + PreparedStatement pst = conn.prepareStatement("update student set name = '小w' where stuId = 331"); + int i = pst.executeUpdate(); + PrintWriter out = resp.getWriter(); + if (i>0){ + out.write("修改成功"); + }else { + out.write("修改失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +package com.qiu.student; + +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.io.PrintWriter; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.SQLException; + +@WebServlet("/delete") +public class Delete extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + PrintWriter out = resp.getWriter(); + Connection conn = DBUtil.getConn(); + try { + PreparedStatement pst = conn.prepareStatement("delete from student where name = '小'"); + int i = pst.executeUpdate(); + if (i>0){ + out.write("删除成功"); + }else { + out.write("删除失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +``` -- Gitee