From 1301b5c58d192931b0827a751f8fb52463d16bba Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=BC=A0=E9=BE=99=E8=85=BE?= <2235304668@qq.com>
Date: Tue, 23 May 2023 22:08:07 +0800
Subject: [PATCH] =?UTF-8?q?servlet=E4=BD=9C=E4=B8=9A?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...230523 Servlet\344\275\234\344\270\232.md" | 219 ++++++++++++++++++
1 file changed, 219 insertions(+)
create mode 100644 "22 \345\274\240\351\276\231\350\205\276/20230523 Servlet\344\275\234\344\270\232.md"
diff --git "a/22 \345\274\240\351\276\231\350\205\276/20230523 Servlet\344\275\234\344\270\232.md" "b/22 \345\274\240\351\276\231\350\205\276/20230523 Servlet\344\275\234\344\270\232.md"
new file mode 100644
index 0000000..c4f8f59
--- /dev/null
+++ "b/22 \345\274\240\351\276\231\350\205\276/20230523 Servlet\344\275\234\344\270\232.md"
@@ -0,0 +1,219 @@
+~~~java
+package servlet;
+
+//工具类
+
+import java.sql.*;
+
+public class BUilt {
+ 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.jdbc.Driver");
+ Connection conn = DriverManager.getConnection(url, user, pass);
+ return conn;
+ }
+
+ public static int currencyUpdate(String sql, String... keys) throws SQLException, ClassNotFoundException {
+ Connection conn = link();
+ PreparedStatement prs = conn.prepareStatement(sql);
+ for (int i = 0; i < keys.length; i++) {
+ prs.setString((i + 1), keys[i]);
+ }
+ int i = prs.executeUpdate();
+ return i;
+ }
+
+ public static int delete(String sql, String... keys) throws SQLException, ClassNotFoundException {
+ int i = currencyUpdate(sql, keys);
+ return i;
+ }
+
+ public static int update(String sql, String... keys) throws SQLException, ClassNotFoundException {
+ int i = currencyUpdate(sql, keys);
+ return i;
+ }
+
+ public static int add(String sql, String... keys) throws SQLException, ClassNotFoundException {
+ int i = currencyUpdate(sql, keys);
+ return i;
+ }
+
+ public static ResultSet Query(String sql, String... keys) throws SQLException, ClassNotFoundException {
+ Connection conn = link();
+ PreparedStatement prs = conn.prepareStatement(sql);
+ for (int i = 0; i < keys.length; i++) {
+ prs.setString((i + 1), keys[i]);
+ }
+ ResultSet rs = prs.executeQuery();
+ return rs;
+ }
+}
+
+~~~
+
+
+
+~~~java
+package 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.*;
+
+@WebServlet("/Servlet/Add")
+public class StudentServlet extends HttpServlet {
+
+ // # 作业,
+// * 1 数据库相关的操作,使用封装的工具类
+// * 2 编写四个servlet,使用注解指定相关的访问路径,分别对应查询,修改,删除,添加的操作
+// * 3 从浏览器中,访问这中个路径,显示响应的信息,查询显示结果,其它的显示成功或是失败
+// * 4 预习题:如何通过浏览器传送请求参数给servlet,servlet如何接收这些参数,并使用这些参数,去影响数据库的操作?
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ resp.setContentType("text/html;charset=utf-8");// 设置网页的格式
+ resp.setCharacterEncoding("utf-8");// 设置网页的编码
+ resp.getWriter().write("这是增加的操作" + "
");
+ // 在这里写java代码
+ try {
+ Class.forName("com.mysql.jdbc.Driver");
+ Connection conn = DriverManager.getConnection("jdbc:mysql:///student?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root");
+ String ADD = "insert into student values (?,'?','?')";
+ PreparedStatement pst = conn.prepareStatement(ADD);
+ int add = BUilt.add("sql", "0", "小明", "男");
+ int re = pst.executeUpdate();
+ if (re > 0) {
+ resp.getWriter().write("添加成功,添加了" + re + "行" + "
");
+ } else {
+ resp.getWriter().write("添加失败!" + "
");
+ }
+
+
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ super.doPost(req, resp);
+ }
+
+}
+
+@WebServlet("/Servlet/Delete")
+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");// 设置网页的编码
+ resp.getWriter().write("这是删除的操作");
+ try {
+ Class.forName("com.mysql.jdbc.Driver");
+ Connection conn = DriverManager.getConnection("jdbc:mysql:///student?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root");
+ String Delete = "delete from student where id=?";
+ int delete = BUilt.delete("sql", "1");
+
+ if (delete > 0) {
+ resp.getWriter().write("删除成功,删除了" + delete + "行" + "
");
+ } else {
+ resp.getWriter().write("删除失败!" + "
");
+ }
+
+
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ super.doPost(req, resp);
+ }
+}
+
+
+@WebServlet("/Servlet/Update")
+class Update extends HttpServlet {
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ resp.setContentType("text/html;charset=utf-8");// 设置网页的格式
+ resp.setCharacterEncoding("utf-8");// 设置网页的编码
+ resp.getWriter().write("这是更新的操作");
+ try {
+ Class.forName("com.mysql.jdbc.Driver");
+ Connection conn = DriverManager.getConnection("jdbc:mysql:///student?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root");
+ String Delete = "update student set sex=? where id=?";
+ int update = BUilt.update("sql", "男", "5");
+
+ if (update > 0) {
+ resp.getWriter().write("更新成功,删除了" + update + "行" + "
");
+ } else {
+ resp.getWriter().write("删除失败!" + "
");
+ }
+
+
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ super.doPost(req, resp);
+ }
+}
+
+
+@WebServlet("/Servlet/Quire")
+class Quire extends HttpServlet {
+ @Override
+ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ resp.setContentType("text/html;charset=utf-8");// 设置网页的格式
+ resp.setCharacterEncoding("utf-8");// 设置网页的编码
+ resp.getWriter().write("这是查询的操作" + "
");
+ // 在这里写java代码
+ try {
+ Class.forName("com.mysql.jdbc.Driver");
+ Connection conn = DriverManager.getConnection("jdbc:mysql:///student?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root");
+ String sql = "select *from student";
+ PreparedStatement pst = conn.prepareStatement(sql);
+ ResultSet re = pst.executeQuery();
+
+ while (re.next()) {
+ int id = re.getInt("id");
+ String name = re.getString("name");
+ String sex = re.getString("sex");
+ System.out.println(id + name + sex);
+
+ resp.getWriter().write("ID:" + id + " 姓名:" + name + " 性别:" + sex + "
");
+ }
+ } catch (ClassNotFoundException e) {
+ throw new RuntimeException(e);
+ } catch (SQLException e) {
+ throw new RuntimeException(e);
+ }
+
+ }
+
+ @Override
+ protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+ super.doPost(req, resp);
+ }
+}
+
+~~~
+
--
Gitee