diff --git "a/13 \345\274\240\345\276\267\345\272\267/20230522 \344\275\234\344\270\232.md" "b/13 \345\274\240\345\276\267\345\272\267/20230522 \344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..4320faca1186c0870fd0753025894db00f7811d3 --- /dev/null +++ "b/13 \345\274\240\345\276\267\345\272\267/20230522 \344\275\234\344\270\232.md" @@ -0,0 +1,37 @@ +```java +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.sql.*; + + +public class Student 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 { + Class.forName("com.mysql.jdbc.Driver"); + Connection conn = DriverManager.getConnection("jdbc://mysql/localhost/3306/student_db?useSSL=false&useUnicode=true&characterEncoding=utf8", "root", "root"); + String sql="insert into student values (4,'唐','女')"; + int i = stmt.executeUpdate(sql); + if (i>0){ + System.out.println("成功添加"); + }else{ + System.out.println("失败"); + } catch (SQLException e) { + throw new RuntimeException(e); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doPost(req, resp); + } +} +``` +