diff --git "a/47 \346\235\216\345\277\265\345\277\265/20230523 \344\275\234\344\270\232.md" "b/47 \346\235\216\345\277\265\345\277\265/20230523 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..1603b1cb5b95aac55f0c5bc40ad31968afa025cb --- /dev/null +++ "b/47 \346\235\216\345\277\265\345\277\265/20230523 \344\275\234\344\270\232.md" @@ -0,0 +1,194 @@ +## 作业: + + 1 数据库相关的操作,使用封装的工具类 + * 2 编写四个servlet,使用注解指定相关的访问路径,分别对应查询,修改,删除,添加的操作 + * 3 从浏览器中,访问这中个路径,显示响应的信息,查询显示结果,其它的显示成功或是失败 + * 4 预习题:如何通过浏览器传送请求参数给servlet,servlet如何接收这些参数,并使用这些参数,去影响数据库的操作? + +~~~ java +//查询 +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.io.PrintWriter; +import java.sql.*; + +@WebServlet("/text") +public class text 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 { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url,username,password); + PreparedStatement pst = conn.prepareStatement("select * from student"); + ResultSet re = pst.executeQuery(); + PrintWriter writer = resp.getWriter(); + writer.write(""); + writer.write(""); + while (re.next()) { + int id = re.getInt(1); + String name = re.getString(2); + String sex = re.getString(3); + writer.write(""); + writer.write(""); + writer.write(""); + } + re.close(); + pst.close(); + conn.close(); + } 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); + } +} +//添加 +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.*; + +@WebServlet("/text") +public class text 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 { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url,username,password); + PreparedStatement pst = conn.prepareStatement("insert into student values (1,'a','男')"); + int i = pst.executeUpdate(); + if (i>0){ + resp.getWriter().write("添加了"+i+"行"); + }else { + System.out.println("添加失败"); + } + pst.close(); + conn.close(); + } 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); + } +} +//修改 +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.*; + +@WebServlet("/text") +public class text 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 { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url,username,password); + PreparedStatement pst = conn.prepareStatement("update student set name = 'a' where name = 'b'"); + int i = pst.executeUpdate(); + if (i>0){ + resp.getWriter().write("修改了"+i+"行"); + }else { + System.out.println("修改失败"); + } + pst.close(); + conn.close(); + } 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); + } +} +//删除 +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.*; + +@WebServlet("/text") +public class text 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 { + Class.forName("com.mysql.jdbc.Driver"); + String url = "jdbc:mysql://localhost:3306/text?useSSL=false&useUnicode=true&characterEncoding=utf8"; + String username = "root"; + String password = "root"; + Connection conn = DriverManager.getConnection(url,username,password); + PreparedStatement pst = conn.prepareStatement("delete from student where id = 1"); + int i = pst.executeUpdate(); + if (i>0){ + resp.getWriter().write("删除了"+i+"行"); + }else { + System.out.println("删除失败"); + } + pst.close(); + conn.close(); + } 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); + } +} +~~~ + diff --git "a/47 \346\235\216\345\277\265\345\277\265/20230524 \344\275\234\344\270\232.md" "b/47 \346\235\216\345\277\265\345\277\265/20230524 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..5c8f6ba5ab10a739f1237e0453d57a53d7fb018e --- /dev/null +++ "b/47 \346\235\216\345\277\265\345\277\265/20230524 \344\275\234\344\270\232.md" @@ -0,0 +1,172 @@ +## 作业: + +~~~ html + + + + + + Title + + + + ID + + +
+ + ID + name + + +
+ + ID + + +
+ + ID + newName + + + + +~~~ + + + +~~~ java +//查询 +import jakarta.servlet.*; +import jakarta.servlet.http.*; +import jakarta.servlet.annotation.*; + +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet(name = "select", value = "/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 { + response.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String id1 = request.getParameter("id"); + ResultSet re = Jdbc.select("select * from student where id=?", id1); + try { + while (re.next()) { + String id = re.getString(1); + String name = re.getString(2); + response.getWriter().write(id+" "+name+"
"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +~~~ + + + +~~~ java +//添加 +import jakarta.servlet.*; +import jakarta.servlet.http.*; +import jakarta.servlet.annotation.*; + +import java.io.IOException; + +@WebServlet(name = "add", value = "/add") +public class add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String id = request.getParameter("id"); + String name = request.getParameter("name"); + int i = Jdbc.update("insert into student values(?,?)",id,name); + if (i>0){ + response.getWriter().write("添加成功"); + }else { + response.getWriter().write("添加失败"); + } + } +} +~~~ + + + +~~~ java +//删除 +import jakarta.servlet.*; +import jakarta.servlet.http.*; +import jakarta.servlet.annotation.*; + +import java.io.IOException; + +@WebServlet(name = "delete", value = "/delete") +public class delete extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String id = request.getParameter("id"); + int i = Jdbc.update("delete from student where id=?",id); + if (i>0){ + response.getWriter().write("删除成功"); + }else { + response.getWriter().write("删除失败"); + } + } +} +~~~ + + + +~~~ java +//修改 +import jakarta.servlet.*; +import jakarta.servlet.http.*; +import jakarta.servlet.annotation.*; + +import java.io.IOException; + +@WebServlet(name = "update", value = "/update") +public class update extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String id = request.getParameter("id"); + String name = request.getParameter("name"); + int i = Jdbc.update("update student set name=? where id=?",name,id); + if (i>0){ + response.getWriter().write("修改成功"); + }else { + response.getWriter().write("修改失败"); + } + } +} +~~~ + diff --git "a/47 \346\235\216\345\277\265\345\277\265/20230527 \344\275\234\344\270\232.md" "b/47 \346\235\216\345\277\265\345\277\265/20230527 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..ac88654a04750b6ae6b9d652cc559b85658e405c --- /dev/null +++ "b/47 \346\235\216\345\277\265\345\277\265/20230527 \344\275\234\344\270\232.md" @@ -0,0 +1,393 @@ +## 作业: + +~~~ java +//工具类 +package Util; + +import java.sql.*; + +public class JDBCUtil { + 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 = ""; + //注册驱动的方法 + //用静态代码块,注册驱动,{}表示代码块,static表示静态的,最先执行 + 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; + } + + /** + * 通用的query + * @param sql 接收的SQL语句 + * @return 将查询的结果集返回给调用者 + */ + 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 rst = pst.executeQuery(); + return rst; + + } + + + + //通用的update + 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 rst){ + try { + if (rst!=null){ + rst.close(); + } + if (pst!=null){ + rst.close(); + } + if (conn!=null){ + rst.close(); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +~~~ + + + +~~~ java +//学生类 +package been; + +public class Student { + private int id; + private String name; + private String sex; + + public Student() { + } + + public Student(int id, String name, String sex) { + + this.id = id; + this.name = name; + this.sex = sex; + } + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", sex='" + sex + '\'' + + '}'; + } +} +~~~ + + + +~~~ java +//修改学生 信息 +package servlet; + +import Util.JDBCUtil; +import been.Student; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet("/updateById") +public class updateById extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //根据ID获取对应的学生信息 + //写servlet要写请求和响应的编码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + //这个servlet要根据ID去数据库删除对应的学生信息 + String id = request.getParameter("id"); + //将得到的ID注入到sql语句 + String sql = "select * from student where id =?"; + try { + ResultSet rs = JDBCUtil.query(sql, id); + //此处得到一个学生的信息,不直接响应,而是封装成一个学生对象,将他转发给一个jsp,让他去处理 + rs.next();//这里不写which循环是因为我们的ID是唯一的,一个ID只有一个学生对象 + int id2 = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + Student stu = new Student(id2, name, sex); + + //将stu对象,添加到request,并转发给负责表单的jsp + request.setAttribute("student",stu); + request.getRequestDispatcher("/edit.jsp").forward(request,response); + + + + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +~~~ + + + +~~~ java +/删除学生信息 +package servlet; + +import Util.JDBCUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet("/deleteById") +public class deleteById extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + //写servlet要写请求和响应的编码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + //这个servlet要根据ID去数据库删除对应的学生信息 + String id = request.getParameter("id"); + //将得到的ID注入到sql语句 + String sql = "delete from student where id =?"; + try { + int i = JDBCUtil.update(sql, id); + if (i>0){ + response.getWriter().write(""); + //成功之后能不能自动调回列表 + //请求有请求转发,响应有重定向 +// response.sendRedirect("/index.jsp"); + + }else{ + response.getWriter().write("删除失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +~~~ + + + +~~~ java +//saveUpdate类 +package servlet; + +import Util.JDBCUtil; + +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("/saveUpdate") +public class saveUpdate extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + doPost(req,resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + //接收表单信息,将信息保存到数据库 + //写servlet要写请求和响应的编码 + req.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); + + + String id = req.getParameter("id"); + String name = req.getParameter("name"); + String sex = req.getParameter("sex"); + + String sql = "update student set name=?,sex=? where id=?"; + int i = 0; + try { + i = JDBCUtil.update(sql, name, sex,id); + } catch (SQLException e) { + throw new RuntimeException(e); + } + if (i>0){ + resp.getWriter().write(""); + //成功之后能不能自动调回列表 + //请求有请求转发,响应有重定向 +// response.sendRedirect("/index.jsp"); + + }else{ + resp.getWriter().write("修改失败"); + } + + } +} + +~~~ + + + +~~~ jsp +//index.jsp +<%@ page import="Util.JDBCUtil" %> +<%@ page import="java.sql.ResultSet" %><%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-05-27 + Time: 15:57 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生列表 + + + +<%-- 使用表格显示学生身份信息--%> +
ID姓名性别
"+id+""+name+""+sex+"
+ + <% + String sql ="select * from student"; + ResultSet rs = JDBCUtil.query(sql); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + //响应给浏览器的 + // response.getWriter().write(id); + // response.getWriter().write(name); + // response.getWriter().write(sex); + // response.getWriter().write("
"); + %> + + + + + + + + <% + } + %> + + + <%-- 具体每个学生的信息,是从数据库里 循环遍历出来的--%> + <%-- jsp中,java代码和html是可以互相嵌套的--%> + +
编号姓名性别操作
<%=id%><%=name%><%=sex%>修改 删除
+ + + +~~~ + + + +~~~ jsp +//edit.jsp +<%@ page import="been.Student" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改学生信息 + + +<% +//从request取出来的学生对象 + Student student = (Student) request.getAttribute("student"); + + //在jsp中有一个标签< %=直接写变量 % > + +%> +<%--<%=student.getId()%>--%> +<%--<%=student.getName()%>--%> +<%--<%=student.getSex()%>--%> + +<%--写表单注意两点:1,谁来接收我们表单的数据 2,以什么方式提交get post--%> + + + 姓名:
+ 性别:
+
+ +
+ + + + + +~~~ +