From 8946be610465844c62ea4f0152711fd0be51f0ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B6=E6=A6=95=E9=94=8B?= <2094447637@qq.com> Date: Tue, 23 May 2023 21:38:21 +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.md" | 195 ++++++++++++++++++ 1 file changed, 195 insertions(+) create mode 100644 "04 \345\217\266\346\246\225\351\224\213/20230522 tomcat.md" diff --git "a/04 \345\217\266\346\246\225\351\224\213/20230522 tomcat.md" "b/04 \345\217\266\346\246\225\351\224\213/20230522 tomcat.md" new file mode 100644 index 0000000..c5d9cd4 --- /dev/null +++ "b/04 \345\217\266\346\246\225\351\224\213/20230522 tomcat.md" @@ -0,0 +1,195 @@ +```markdown +# 作业, + * 1 数据库相关的操作,使用封装的工具类 + * 2 编写四个servlet,使用注解指定相关的访问路径,分别对应查询,修改,删除,添加的操作 + * 3 从浏览器中,访问这中个路径,显示响应的信息,查询显示结果,其它的显示成功或是失败 + * 4 预习题:如何通过浏览器传送请求参数给servlet,servlet如何接收这些参数,并使用这些参数,去影响数据库的操作? + +``` + +工具类 + +```java +import java.sql.*; +public class DbJbdc { + private static final String url="jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String user="root"; + private static final String pass="root"; + public static Connection link() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.cj.jdbc.Driver"); + Connection conn = DriverManager.getConnection(url, user, pass); + return conn; + } + 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 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.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("text/html;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+"
"); + } + } catch (SQLException e) { + e.printStackTrace(); + } catch (ClassNotFoundException e) { + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +----------------------------------------------------------- +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 { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + try { + String sql ="insert into student values (?,?,?)"; + int add = DbJbdc.add(sql, "0", "小明", "男"); + if (add>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +------------------------------------------------------------ +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 { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + try { + String sql ="delete from student where id=?"; + int add = DbJbdc.add(sql, "1"); + if (add>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +------------------------------------------------------------ +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("/modify") +public class Modify extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + try { + String sql ="update student set sex=? where id=?"; + int add = DbJbdc.add(sql, "女","2"); + if (add>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} + +``` \ No newline at end of file -- Gitee