From f678a41d8e000365fd483cb2a586b46b488af072 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E7=8E=AE=E9=94=8B?= <295357613@qq.com> Date: Sun, 28 May 2023 22:27:24 +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 --- ...50\345\215\225\344\277\256\346\224\271.md" | 275 ++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 "17\351\203\221\347\216\256\351\224\213/20230527\350\241\250\345\215\225\344\277\256\346\224\271.md" diff --git "a/17\351\203\221\347\216\256\351\224\213/20230527\350\241\250\345\215\225\344\277\256\346\224\271.md" "b/17\351\203\221\347\216\256\351\224\213/20230527\350\241\250\345\215\225\344\277\256\346\224\271.md" new file mode 100644 index 0000000..85b4a6e --- /dev/null +++ "b/17\351\203\221\347\216\256\351\224\213/20230527\350\241\250\345\215\225\344\277\256\346\224\271.md" @@ -0,0 +1,275 @@ +DBUtil + +```java +package utils; +import java.sql.*; + +/** + * 工具类 + */ +public class DBUtil { + //1.设置主机,端口号,数据库,关闭ssl,启用unicode和utf8,用户名,密码 + private static final String url="jdbc:mysql:///aa?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String name="root"; + private static final String pwd="root"; + 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, name, pwd); + return conn; + } + 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; + } + +} +``` + +deleteById + +```java +package servlet; +/** + * 删除按钮 + */ + +import utils.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +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 { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String id = request.getParameter("id"); + String sql = "delete from student where id =?"; + try { + int i = DBUtil.update(sql, id); + if (i>0){ + response.getWriter().write(""); + }else { + response.getWriter().write("删除失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} +``` + +saveUpdate + +```java +package servlet; + +import utils.DBUtil; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.SQLException; + +/** + * 修改按钮 + */ +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 { + super.doPost(req, resp); + 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="updete student set name=?,sex=?,sex=? where id=?"; + + int i = 0; + try { + i = DBUtil.update(sql, id, name, sex); + } catch (SQLException e) { + throw new RuntimeException(e); + } + if (i>0){ + resp.getWriter().write(""); + + }else { + resp.getWriter().write("删除失败"); + } + } +} +``` + +updateById + +```java +package servlet; + +import bean.Student; +import utils.DBUtil; + +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;charset=utf-8"); + String id = request.getParameter("id"); + + + String sql="select * from student where id=?"; + try { + ResultSet rs = DBUtil.query(sql, id); + rs.next(); + int id2 = rs.getInt("id"); + String name= rs.getString("name"); + String sex=rs.getString("sex"); + Student stu = new Student(id2, name, sex); + + 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 { + + } +} +``` + +```jsp +<%@ page import="java.sql.ResultSet" %> +<%@ page import="utils.DBUtil" %><%-- + Created by IntelliJ IDEA. + User: 锋垒8 + Date: 2023/5/28 + Time: 21:37 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 学生列表 + + + + + + + + + + + <% + String sql = "select * from student"; + ResultSet rs = DBUtil.query(sql); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + %> + + + + + + + <% + } + %> + +
编号姓名性别操作
<%=id%><%=name%><%=sex%>修改 删除
+ + +``` + +```jsp +<%@ page import="bean.Student" %><%-- + Created by IntelliJ IDEA. + User: 锋垒8 + Date: 2023/5/28 + Time: 21:37 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + 修改学生信息 + + +<% + Student student = (Student) request.getAttribute("student"); +%> + +<%=student.getId()%> +<%=student.getName()%> +<%=student.getSex()%> + +
+ 编号:

+姓名:
+性别:
+ + + + +``` + -- Gitee