zoukankan      html  css  js  c++  java
  • 使用servlet连接数据库删除数据

    1、所有数据渲染浏览器

    import javax.jws.WebService;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.sql.*;
    
    @WebServlet("/sss")
    public class GetDateServlet extends HttpServlet {
        @Override
        protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setCharacterEncoding("UTF-8");
            resp.setContentType("text/html;charset=utf8");
            PrintWriter out = resp.getWriter();
            Connection conn=null;
            Statement stm=null;
            ResultSet rs=null;
            String date="";
            try {
                Class.forName("org.sqlite.JDBC");
                conn= DriverManager.getConnection("jdbc:sqlite:D:/sqlite/date/Student.sqlite");
                stm=conn.createStatement();
                rs=stm.executeQuery("select name,score,id from StuInfo");
                while (rs.next()){
                    date+="<tr><td>"+rs.getString(1)+"</td><td>"+rs.getInt(2)+
                            "</td><td class='del'><a href='/test_war_exploded/del?id="
                            +rs.getInt(3)+"'>删除</a></td></tr>";
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    if (rs!=null) rs.close();
                    if (stm!=null) stm.close();
                    if (conn!=null) conn.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
            out.println("<html>");
            out.println("<head>");
            out.println("<style>");
            out.println("table{border-collapse:collapse;300px;margin:auto;text-align:center;}");
            out.println("table,tr,th,td{border:1px solid #000}");
            out.println("</style>");
            out.println("</head>");
            out.println("<body>");
            out.println("<table>");
            out.println(date);
            out.println("</table>");
            out.println("<script>");
            out.println("<script>");
            out.println("</body>");
            out.println("</html>");
    
        }
    }

    2、根据提交方式获取id然后删除数据库

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.sql.*;
    
    @WebServlet("/del")
    public class DelServlet extends HttpServlet {
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            String id=req.getParameter("id");
            Connection conn=null;
            Statement stm=null;
            try {
                Class.forName("org.sqlite.JDBC");
                conn= DriverManager.getConnection("jdbc:sqlite:D:/sqlite/date/Student.sqlite");
                stm=conn.createStatement();
                stm.executeUpdate("delete from StuInfo where id="+id);
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    if (stm!=null) stm.close();
                    if (conn!=null) conn.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
            req.getRequestDispatcher("/sss").forward(req,resp);
        }
    
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    
        }
    }

    3、完成,运行

  • 相关阅读:
    【链表】Remove Duplicates from Sorted List II(三指针)
    【链表】Reorder List
    【链表】 Reverse Linked List II
    【链表】Rotate List(三个指针)
    【链表】Sort List(归并排序)
    【链表】Swap Nodes in Pairs(三指针)
    数组中常用算法(方法)总结
    使用 gitee 托管你的 go 模块
    git库移植
    git 访问方式浅谈
  • 原文地址:https://www.cnblogs.com/LixiaoFeng1650062546/p/11138799.html
Copyright © 2011-2022 走看看