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、完成,运行

  • 相关阅读:
    MyEclipse添加SVN插件
    Postgresql的character varying = bytea问题
    Hibernate主键增加方式
    java配置环境变量
    Maven常用构建命令
    Postgresql的主键自增长
    js判断金额
    最精简的SQL教程
    SQL练习1:统计班级男女生人数
    sql 百万级数据库优化方案
  • 原文地址:https://www.cnblogs.com/LixiaoFeng1650062546/p/11138799.html
Copyright © 2011-2022 走看看