diff --git "a/12 \350\213\217\344\273\244\351\271\217/20230522_12 \350\213\217\344\273\244\351\271\217 idea\345\220\257\345\212\250tomcat.md" "b/12 \350\213\217\344\273\244\351\271\217/20230522_12 \350\213\217\344\273\244\351\271\217 idea\345\220\257\345\212\250tomcat.md" new file mode 100644 index 0000000000000000000000000000000000000000..2d148c879265555c3600d7460667fec9c675c632 --- /dev/null +++ "b/12 \350\213\217\344\273\244\351\271\217/20230522_12 \350\213\217\344\273\244\351\271\217 idea\345\220\257\345\212\250tomcat.md" @@ -0,0 +1,40 @@ +```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 = "select * from student"; + PreparedStatement pst = conn.prepareStatement(sql); + ResultSet rs = pst.executeQuery(); + while (rs.next()){ + String id = rs.getString("id"); + String name = rs.getString("name"); + String sex = rs.getString("sex"); + resp.getWriter().write("id:"+id+",name:"+name+",sex:"+sex); + } + } 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); + } +} +``` +