From d578306c04bd0fbca49ebbd40ac50e960ef794f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BC=BA?= <“3108497868@qq.com”> Date: Fri, 26 May 2023 22:58:04 +0800 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 --- .../230524.jsp\344\275\234\344\270\232.md" | 225 ++++++++++++++++++ 1 file changed, 225 insertions(+) create mode 100644 "45 \347\216\213\345\274\272/230524.jsp\344\275\234\344\270\232.md" diff --git "a/45 \347\216\213\345\274\272/230524.jsp\344\275\234\344\270\232.md" "b/45 \347\216\213\345\274\272/230524.jsp\344\275\234\344\270\232.md" new file mode 100644 index 0000000..8bf2898 --- /dev/null +++ "b/45 \347\216\213\345\274\272/230524.jsp\344\275\234\344\270\232.md" @@ -0,0 +1,225 @@ +```java + +```java +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 { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + System.out.println("wuwuwu"); + request.setCharacterEncoding("utf-8"); + String id = request.getParameter("id"); + String name = request.getParameter("name"); + String sex = request.getParameter("sex"); + + System.out.println("id = " + id); + System.out.println("name = " + name); + System.out.println("sex = " + sex); + + String sql="insert into stu values (?,?,?)"; + response.setContentType("text/html;charset=utf-8"); + try { + int i = DBUtil.update(sql, id, name, sex); + if (i>0){ + response.getWriter().write("添加成功"); + }else { + response.getWriter().write("添加失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + + } +} + +``` + +```java +public class DBUtil { + // 先创建一个数据库的工具类,可以将注册驱动,连接对象,等等都封装成对应的方法 + private static final String url="jdbc:mysql://localhost:3306/student?useSSL=false&useUnicode=true&characterEncoding=utf8"; + private static final String username="root"; + private static final String pwd="123456789"; + + //用静态代码块注册驱动 + static { + try { + Class.forName("com.mysql.jdbc.Driver"); + } catch (ClassNotFoundException e) { + throw new RuntimeException(e); + } + } + //获取连接对象 + private static Connection getConn() throws SQLException { + Connection conn = DriverManager.getConnection(url, username, pwd); + 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++) { + 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); + for (int i = 0; i < keys.length; i++) { + pst.setString((i+1),keys[i]); + } + int i= pst.executeUpdate(); + return i; + + } + //通用的释放资源的方法 + private 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(); + } + + } +} + +``` + +```java +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 { + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String id = request.getParameter("id"); + String sql="delete from stu where id=?"; + response.setContentType("text/html;charset=utf-8"); + try { + int i = DBUtil.update(sql,id); + if (i>0){ + response.getWriter().write("修改成功"); + }else { + response.getWriter().write("修改失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + } +} + +``` + +```java +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +import java.sql.ResultSet; +import java.sql.SQLException; + +@WebServlet("/Servlet") +public class Servlet extends HttpServlet { + @Override + protected void doGet(javax.servlet.http.HttpServletRequest req, javax.servlet.http.HttpServletResponse resp) throws ServletException, IOException { + System.out.println("DoGet is running ......."); + resp.setContentType("text/html;charset=utf-8"); + + String sql="select * from stu"; + try { + ResultSet rst = DBUtil.query(sql); + while (rst.next()){ + int id = rst.getInt("id"); + String name = rst.getString("name"); + String sex = rst.getString("sex"); + resp.getWriter().write(id+name+sex); + } + } catch (SQLException e) { + e.printStackTrace(); + } + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + + } +} + +``` + +```java +import javax.servlet.*; +import javax.servlet.http.*; +import javax.servlet.annotation.*; +import java.io.IOException; +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 { + + + } + + @Override + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + request.setCharacterEncoding("utf-8"); + String id = request.getParameter("id"); + String name = request.getParameter("name"); + String sex = request.getParameter("sex"); + String sql="update stu set name=? , sex=? where id=?"; + response.setContentType("text/html;charset=utf-8"); + try { + int i = DBUtil.update(sql, name, sex,id); + if (i>0){ + response.getWriter().write("修改成功"); + }else { + response.getWriter().write("修改失败"); + } + } catch (SQLException e) { + throw new RuntimeException(e); + } + + } +} +``` +``` -- Gitee