diff --git "a/40 \346\226\207\346\231\272\345\213\207/20230527 JDBC\344\275\234\344\270\232.md" "b/40 \346\226\207\346\231\272\345\213\207/20230527 JDBC\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..3acf58283a880399bd137c59bd5bf1cc41188356 --- /dev/null +++ "b/40 \346\226\207\346\231\272\345\213\207/20230527 JDBC\344\275\234\344\270\232.md" @@ -0,0 +1,258 @@ +```jsp +<%@ page import="Tool.DbJbdc" %> +<%@ page import="java.sql.ResultSet" %> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生信息 + + + + + + <% + 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"); + + %> + + + + + + + + + <% + } + %> + + +
编号姓名性别操作
<%=id%><%=name%><%=sex%>修改 删除
+ + + +``` + +```java +package servlet; + +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; + } +} + +``` + +```java +package servlet; + +import Tool.DbJbdc; +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.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 { + response.setContentType("text/html;charset=utf-8"); + request.setCharacterEncoding("utf-8"); + + String id = request.getParameter("id"); + String sql ="select * from student where id=?"; + try { + ResultSet rs = DbJbdc.Query(sql, id); + rs.next(); + int id1 = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + student stu = new student(id1, name, sex); + request.setAttribute("student",stu); + request.getRequestDispatcher("/edit.jsp").forward(request,response); + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```java +package servlet; + +import Tool.DbJbdc; +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.SQLException; + + +@WebServlet(name = "deleteById", value = "/deleteById") +public class deleteById extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("text/html;charset=utf-8"); + request.setCharacterEncoding("utf-8"); + String id = request.getParameter("id"); + String sql="delete from student where id=?"; + try { + int i = DbJbdc.Update(sql, id); + if (i>0){ + response.getWriter().write(""); +// response.sendRedirect("/index.jsp"); + }else { + response.getWriter().write("删除失败"); + } + } catch (SQLException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```java +package servlet; + +import Tool.DbJbdc; +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.SQLException; + + +@WebServlet(name = "saveUpdate", value = "/saveUpdate") +public class saveUpdate 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.setContentType("text/html;charset=utf-8"); + request.setCharacterEncoding("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=?"; + try { + int i = DbJbdc.Update(sql, name, sex,id); + if (i>0){ + response.getWriter().write(""); + // response.sendRedirect("/index.jsp"); + }else { + response.getWriter().write("修改失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } +} + +``` + + + +```jsp +<%@ page import="servlet.student" %><%-- + Created by IntelliJ IDEA. + User: Administrator + Date: 2023-05-27 + Time: 16:55 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改学生 + + +<% + student stu = (student) request.getAttribute("student"); +%> +
+ 姓名
+ 性别
+ + +
+ + + +``` +