diff --git "a/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/gongju/DBUtil.java" "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/gongju/DBUtil.java" new file mode 100644 index 0000000000000000000000000000000000000000..e21fb4c08dcf592cc1d75f8358ba8ce5546c7303 --- /dev/null +++ "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/gongju/DBUtil.java" @@ -0,0 +1,83 @@ +package gongju; + +import java.sql.*; + +/** + * 这是一个数据库的工具类,可以将注册驱动,连接对象,查询,添,删,改都封装成相应的方法 + */ +public class DBUtil { + //1.5 设置主机,端口号,数据库,关闭SSL,启用Unicode和utf8 、用户名,密码 + private static final String url="jdbc:mysql://localhost:3306/student_db?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 { + //2 获取连接的对象conn + Connection conn = DriverManager.getConnection(url, name, pwd);// Alt+回车 + return conn; + } + + // + + /** + * 通用的查询方法 + * @param sql 接收的SQL语句 + * @return 将查询的结果集返回给调用者 + * String ...keys :...表示可变的参数,0个或是任意个数的参数都可以 + */ + public static ResultSet query(String sql,String ...keys) throws SQLException { + Connection conn = getConn(); + PreparedStatement pst = conn.prepareStatement(sql); + // 设置?的值 + // pst.setString(1,userid); + // pst.setString(2,username); + for (int i = 0; i < keys.length; i++) { + // String key = keys[i];// id ,name + 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); + // 设置?的值 + // pst.setString(1,userid); + // pst.setString(2,username); + for (int i = 0; i < keys.length; i++) { + // String key = keys[i];// id ,name + pst.setString((i+1),keys[i]); + } + int i = pst.executeUpdate(); + return i; + } + + + + // 通用的释放资源的方法 + public static void close(Connection conn,PreparedStatement pst,ResultSet rst) throws SQLException { + if (rst!=null){ + rst.close(); + } + if (pst!=null){ + pst.close(); + } + if (conn!=null){ + conn.close(); + } + } +} diff --git "a/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/xuesheng/Student.java" "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/xuesheng/Student.java" new file mode 100644 index 0000000000000000000000000000000000000000..b63a37b123778afaba6c813379817a433d0124c2 --- /dev/null +++ "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/xuesheng/Student.java" @@ -0,0 +1,49 @@ +package xuesheng; + +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 + '\'' + + '}'; + } +} diff --git "a/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/Servletupdate.java" "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/Servletupdate.java" new file mode 100644 index 0000000000000000000000000000000000000000..fe251bb9b99a87a651eb4471e06f2b9ab4b21abb --- /dev/null +++ "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/Servletupdate.java" @@ -0,0 +1,38 @@ +package yingyong; + +import gongju.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet(name = "Servletupdate", value = "/Servletupdate") +public class Servletupdate extends HttpServlet { + @Override + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + doPost(request,response); + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + response.setContentType("text/html;charset=utf-8"); + String name = request.getParameter("name"); + String sex = request.getParameter("sex"); + String id = request.getParameter("id"); + String sql = "update student set name = ?,sex = ? where id = ?"; + int i = 0; + try { + i = DBUtil.update(sql, name, sex, id); + if (i>0){ + response.getWriter().write(""); + }else { + response.getWriter().write("修改失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} diff --git "a/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/add.java" "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/add.java" new file mode 100644 index 0000000000000000000000000000000000000000..51a73e322c9ebfa4ba99e18b6d3195194a4d6acc --- /dev/null +++ "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/add.java" @@ -0,0 +1,35 @@ +package yingyong; + +import gongju.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet(name = "add", value = "/add") +public class add 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 sql = "insert into student values(1,'张三','男'),(2,'李四','女'),(3,'王五','男');"; + response.setCharacterEncoding("utf-8"); + try { + int i = DBUtil.update(sql); + 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 { + + } +} diff --git "a/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/delete.java" "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/delete.java" new file mode 100644 index 0000000000000000000000000000000000000000..830481709321c3eca1f2420d6696ea0c8f95d61e --- /dev/null +++ "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/delete.java" @@ -0,0 +1,38 @@ +package yingyong; + +import gongju.DBUtil; + +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.SQLException; + +@WebServlet(name = "delete", value = "/delete") +public class delete 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 sql = "delete from student where id = ?"; + String id = request.getParameter("id"); + response.setCharacterEncoding("utf-8"); + try { + int i = DBUtil.update(sql, id); + if (i>0){ +// response.getWriter().write("删除成功"); + + 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 { + + } +} diff --git "a/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/update.java" "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/update.java" new file mode 100644 index 0000000000000000000000000000000000000000..09793767409597139ea892edc781310f916a327d --- /dev/null +++ "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/src/yingyong/update.java" @@ -0,0 +1,40 @@ +package yingyong; + +import gongju.DBUtil; +import xuesheng.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 = "update", value = "/update") +public class update 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 re = DBUtil.query(sql, id);//结果集 + re.next();//如果没写会显示sql. sqlexception:在结果集开始之前 + int id1 = re.getInt("id"); + String name = re.getString("name"); + String sex = re.getString("sex"); + Student st = new Student(id1, name, sex); + request.setAttribute("student",st);//封装好的学生类 + 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 { + + } +} diff --git "a/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/web/WEB-INF/html.html" "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/web/WEB-INF/html.html" new file mode 100644 index 0000000000000000000000000000000000000000..566549bdf8fae810809c1a81066000687cb338f6 --- /dev/null +++ "b/21 \345\210\230\345\260\221\346\265\267/2023 0527 \347\275\221\351\241\265\345\212\237\350\203\275/web/WEB-INF/html.html" @@ -0,0 +1,10 @@ + + +
+ +编号 | 姓名 | 性别 | 选择 |
---|---|---|---|
<%=id%> | +<%=name%> | +<%=sex%> | ++ 修改 + 删除 + | +