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

📄 adminuserservlet.java

📁 jsp+javabean写的论坛
💻 JAVA
字号:
package com.alumni.servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.jsp.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import com.alumni.bean.*;

public class AdminUserServlet extends HttpServlet
{
	ServletConfig config;
	//初始化
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.config=config;
	}
	//调用doPost方法
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		this.doPost(request,response);
	}
	//doPost核心方法
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		DBConnection dbConn=null;
		Connection conn=null;
		Statement stmt=null;
		
		String serverUrl=null;
		String[] delete=null;
		
		//session
		HttpSession session=request.getSession();
		//out
		PrintWriter out=response.getWriter();
		//application
		ServletContext application=this.getServletConfig().getServletContext();

		//取得服务器路径
		serverUrl=application.getRealPath("/upload/uploadPic/");
		
		//判断是否登陆过
		Boolean hasLogin=(Boolean)session.getAttribute("hasLogin");
		if(hasLogin!=null&&hasLogin.booleanValue()==true)
		{
			try
			{
				//连接到数据库
				dbConn=new DBConnection();
				conn=dbConn.getConnectionToAccess(application.getRealPath("db/alumni.mdb"));
				stmt=conn.createStatement();
				
				//接收参数
				delete=request.getParameterValues("userID");
				if(delete!=null)
				{
					//先查找并删除该用户相关的图片
					for(int i=0;i<delete.length;i++)
					{
						String search="SELECT PIC_NAME FROM UPLOADPIC WHERE USER_ID="+delete[i];
						ResultSet tempRs=stmt.executeQuery(search);
						while(tempRs.next())
						{
							String fileUrl=serverUrl+"\\"+tempRs.getString("PIC_NAME");
							File deleteFile=new File(fileUrl);
							if(deleteFile.exists())
							{
								//如果文件存在就删除它
								deleteFile.delete();
							}
							else
							{
								if(dbConn!=null)
								{
									dbConn.close();
								}
								response.sendRedirect("/error.jsp?code=FileNotFoundException");
							}
						}
					}
					
					//组合查询
					StringBuffer makeSql=new StringBuffer();
					for(int i=0;i<delete.length;i++)
					{
						makeSql.append(delete[i]);
						if(i!=delete.length-1)
						{
							makeSql.append(",");
						}
					}
					//删除帖子
					String sqlSubject="DELETE FROM SUBJECT WHERE USER_ID IN("+makeSql.toString()+")";
					//删除数据库图片的记录
					String sqlPic="DELETE FROM UPLOADPIC WHERE USER_ID IN("+makeSql.toString()+")";
					//删除用户
					String sqlUser="DELETE FROM USERINFO WHERE ID IN("+makeSql.toString()+")";
					
					stmt.executeUpdate(sqlSubject);
					stmt.executeUpdate(sqlPic);
					stmt.executeUpdate(sqlUser);
					
					//关闭数据库
					if(dbConn!=null)
					{
						dbConn.close();
					}
					response.sendRedirect("/userlist.jsp");
				}
			}
			catch(SQLException sqle)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=SQLException");
			}
			catch(ClassNotFoundException cnfe)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=ClassNotFoundException");
			}
			catch(Exception e)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=UnknownException");
			}
		}
	}
}

⌨️ 快捷键说明

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