diff --git "a/34 \347\250\213\351\230\263/20230527JSP/Jsp.md" "b/34 \347\250\213\351\230\263/20230527JSP/Jsp.md" new file mode 100644 index 0000000000000000000000000000000000000000..c98adeea30ab9e7054c686a850b94115a6e641a1 --- /dev/null +++ "b/34 \347\250\213\351\230\263/20230527JSP/Jsp.md" @@ -0,0 +1,334 @@ +```java +<%@ page import="java.sql.ResultSet" %> +<%@ page import="Utils.JDBC" %><%-- + //index.jsp + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-05-27 + Time: 15:27 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生列表 + + + + + + + + + + + <% + String sql = "select *from student"; + ResultSet select = JDBC.select(sql); + while (select.next()) { + int id = select.getInt("id"); + String name = select.getString("name"); + String sex = select.getString("sex"); + %> + + + + + + + + <% + } + %> +
编号姓名年龄操作
<%=id%> + <%=name%> + <%=sex%> + 修改 删除
+ + + +<%@ page import="use.Student" %><%-- + Created by IntelliJ IDEA. + //Update.jsp + User: 阳 + Date: 2023/5/28 + Time: 20:38 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改学生信息 + + +<% + // 从request取出传过来的学生对象 +// 强转,因为默认是Object +%> + +<% + Student student = (Student) request.getAttribute("student"); +%> + +
+ 编号:
+ 姓名:
+ 性别:
+ +
+ + + +//工具类 +package Utils; +import java.sql.*; + +public class JDBC { + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + //创建连接 + private static Connection conn() { + String url = "jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=utf8"; + Connection conn = null; + try { + conn = DriverManager.getConnection(url, "root", "36932211"); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return conn; + } + + //查询 + public static ResultSet select(String sql, String... arr) { + ResultSet re = null; + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i + 1), arr[i]); + } + re = pst.executeQuery(); + } catch (SQLException e) { + throw new RuntimeException(e); + } + return re; + } + + //增删改 + public static int update(String sql, String... arr) { + try { + PreparedStatement pst = conn().prepareStatement(sql); + for (int i = 0; i < arr.length; i++) { + pst.setString((i + 1), arr[i]); + } + int i = pst.executeUpdate(); + return i; + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} +//封装的学生对象 +package use; + +public class Student { + private int id; + private String name; + private String sex; + + @Override + public String toString() { + return "Student{" + + "id=" + id + + ", name='" + name + '\'' + + ", 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; + } + + public Student() { + } + + public Student(int id, String name, String sex) { + this.id = id; + this.name = name; + this.sex = sex; + } +} + +package Servlet; +//delete Servlet +import Utils.JDBC; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + +@WebServlet(name = "deletebyid", value = "/deletebyid") +public class deletebyid extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// 设置处理乱码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;character=utf-8"); +// 接受网页参数 + String id=request.getParameter("id"); +// 设置sql语句 + String sql="delete from student where id=?"; + response.setCharacterEncoding("utf-8"); +// 处理结果 + try { + int i = JDBC.update(sql,id); + if (i>0){ + response.getWriter().write(""); + + }else { + response.getWriter().write("删除失败"); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +package Servlet; +//update Servlet + +import Utils.JDBC; +import use.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(name = "updatebyid", value = "/updatebyid") +public class updatebyid extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + // 设置处理乱码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;character=utf-8"); +// 根据id去更改信息 + String id=request.getParameter("id"); + + String sql="select *from student where id=?"; + try { + ResultSet rs = JDBC.select(sql, id); +// 因为查出来的id只有一个,所以不用while循环了 +// 这里得到学生信息,不直接响应,可以封装成一个学生对象。 +// 然后再将它转发给一个jsp页面,让他去处理显示 + rs.next(); + int stuid = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + Student stu = new Student(stuid,name,sex); + +// 将Student对象,添加到request。并转发给负责显示表单的jsp + request.setAttribute("student",stu); +// + request.getRequestDispatcher("/update.jsp").forward(request,response); + + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +package Servlet; +//saveUpdate Servlet + +import Utils.JDBC; + + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; + + +@WebServlet(name = "saveUpdate", value = "/saveUpdate") +public class savaUpdate extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// 将doGet转发给doPost,这样两个都可以用 + doPost(request, response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { +// 接受表单信息,将数据保存到数据库 + // 设置处理乱码 + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;character=utf-8"); + + String id = request.getParameter("id"); + String name = request.getParameter("name"); + String sex = request.getParameter("sex"); + + String sql = "update student set name=?,sex=? where id=?"; + response.setCharacterEncoding("utf-8"); +// 处理结果 + try { + int i = JDBC.update(sql,name,sex,id); + if (i>0){ + response.getWriter().write(""); + + }else { + response.getWriter().write("修改失败"); + } + } catch (IOException e) { + throw new RuntimeException(e); + } + + } +} +``` +