使用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;width: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、完成,运行

posted @ 2019-07-05 15:51  不希望有明天  阅读(1884)  评论(0编辑  收藏  举报