diff --git "a/40 \346\226\207\346\231\272\345\213\207/20230524 .md" "b/40 \346\226\207\346\231\272\345\213\207/20230524 .md" new file mode 100644 index 0000000000000000000000000000000000000000..ba9176d4afe5b61538982cacda73cb74789c1852 --- /dev/null +++ "b/40 \346\226\207\346\231\272\345\213\207/20230524 .md" @@ -0,0 +1,249 @@ +```html + + + + + 测试 + + +

添加学生

+
+ 学号
+ 姓名
+ 性别
+ +
+ +

修改学生信息

+
+ 请输入要修改学生的学号
+ 姓名
+ + +
+ +

删除学生

+
+ 请输入需要删除学生的学号

+ +
+ +

查找学生

+
+ 请输入需要查询的学生编号 + +
+ + +``` + +```java +public class DbJbdc { + private static final String url="jdbc:mysql:///student_db?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String user="root"; + private static final String pass="200311"; + public static Connection link() throws ClassNotFoundException, SQLException { + Class.forName("com.mysql.cj.jdbc.Driver"); + return DriverManager.getConnection(url, user, pass); + } + public static int currencyUpdate(String sql,String ...flop) throws SQLException, ClassNotFoundException { + Connection conn = link(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setString((i+1),flop[i]); + } + int i = prs.executeUpdate(); + return i; + } + public static int delete(String sql,String ...flop) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, flop); + return i; + } + public static int modify(String sql,String ...flop) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, flop); + return i; + } + public static int add(String sql,String ...flop) throws SQLException, ClassNotFoundException { + int i = currencyUpdate(sql, flop); + return i; + } + public static ResultSet Query(String sql,String ...flop) throws SQLException, ClassNotFoundException { + Connection conn = link(); + PreparedStatement prs = conn.prepareStatement(sql); + for (int i = 0; i < flop.length; i++) { + prs.setString((i+1),flop[i]); + } + ResultSet rs = prs.executeQuery(); + return rs; + } +} +``` + +```java + +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("/seek") +public class seek extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + try { + 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"); + resp.getWriter().write("编号:"+id+"姓名:"+name+"性别:"+sex+"
"); + } + } catch (SQLException | ClassNotFoundException e) { + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setCharacterEncoding("utf-8"); + resp.setContentType("text/html;charset=utf-8"); + try { + String idbh = req.getParameter("id"); + ResultSet rs = DbJbdc.Query("select * from student where id=?", idbh); + while (rs.next()){ + int id = rs.getInt("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + resp.getWriter().write("编号:"+id+"姓名:"+name+"性别:"+sex+"
"); + } + } catch (SQLException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java + +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("/add") +public class add extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + String idbh = req.getParameter("id"); + String namebh = req.getParameter("name"); + String sexbh = req.getParameter("sex"); + try { + int add = DbJbdc.add("insert into student values (?,?,?);", idbh, namebh, sexbh); + if (add>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java +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("/delete") +public class delete extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + String idbh = req.getParameter("id"); + try { + int delete = DbJbdc.delete("delete from student where id=?", idbh); + if (delete>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java +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("/modify") +public class modify extends HttpServlet { + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doGet(req, resp); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.setContentType("text/html;charset=utf-8"); + resp.setCharacterEncoding("utf-8"); + String idbh = req.getParameter("id"); + String namebh = req.getParameter("name"); +// String sexbh = req.getParameter("sex"); + try { + int modify = DbJbdc.modify("update student set name=? where id=?", namebh,idbh); + if (modify>0){ + resp.getWriter().write("成功"); + }else { + resp.getWriter().write("失败"); + } + } catch (SQLException | ClassNotFoundException e) { + throw new RuntimeException(e); + } + } +} + +``` +