⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 delete.java

📁 采用jsp+servlet+javabean
💻 JAVA
字号:
package guestbook;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import javax.sql.DataSource;
import java.sql.*;

public class delete extends HttpServlet {
  private static final String CONTENT_TYPE = "text/html; charset=GB2312";
  private DataSource dataSource=null;
  //Initialize global variables
  public void init() throws ServletException {
    Init init=new Init(getServletContext().getRealPath("/"));
    dataSource=LinkDB.getDB();
    if (dataSource==null) {
      LinkDB.setDB(init.getDriverName(),init.getDBURL(),init.getDBUser(),init.getDBPassword());
      dataSource=LinkDB.getDB();
    }
  }
  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    if(!Head.checkAdminLogin(request)){
      out.println("<script>alert('无管理权限');document.location='index.jsp';</script>");
      return;
    }
    int id;
    try{
      id = Integer.parseInt(request.getParameter("id"));
    }
    catch(NumberFormatException e){
      throw new RuntimeException("错误的ID号");
    }
    Connection conn=null;
    Statement stmt=null;
    try{
      conn=dataSource.getConnection();
      stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
      int num=stmt.executeUpdate("delete from guestbook where id='"+id+"'");
      if(num==0){
        out.println("<script>alert('删除失败,可能ID不存在');document.location='index.jsp';</script>");
      }
      else{
        out.println("<script>alert('删除成功');document.location='index.jsp';</script>");
      }
    }
    catch(SQLException e){
      out.println(e);
    }
    finally{
      try{
        stmt.close();
        conn.close();
        LinkDB.shutdownDataSource(dataSource);
      }
      catch(SQLException e){
        out.println(e);
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -