From bf868bb4cdc9ce273fef3a024a8b553260773799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E5=BF=97=E9=B9=8F?= <326806914@qq.com> Date: Sun, 28 May 2023 13:35:49 +0000 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 Signed-off-by: 朱志鹏 <326806914@qq.com> --- ...44\345\222\214\344\277\256\346\224\271.md" | 206 ++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 "10 \346\234\261\345\277\227\351\271\217/\345\210\240\351\231\244\345\222\214\344\277\256\346\224\271.md" diff --git "a/10 \346\234\261\345\277\227\351\271\217/\345\210\240\351\231\244\345\222\214\344\277\256\346\224\271.md" "b/10 \346\234\261\345\277\227\351\271\217/\345\210\240\351\231\244\345\222\214\344\277\256\346\224\271.md" new file mode 100644 index 0000000..c5648f2 --- /dev/null +++ "b/10 \346\234\261\345\277\227\351\271\217/\345\210\240\351\231\244\345\222\214\344\277\256\346\224\271.md" @@ -0,0 +1,206 @@ +```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%> + 修改 删除
+ + +``` + +```java +<%@ page import="use.Student" %><%-- + Created by IntelliJ IDEA. + //Update.jsp + User: 苏晓 + Date: 2023/5/28 + Time: 17:56 + 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"); +%> + +
+ 编号:
+ 姓名:
+ 性别:
+ + + +
+ + + + + + + +``` + +```java +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; + } +} +``` + +````ja -- Gitee