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

📄 admin.java

📁 挺好的东东
💻 JAVA
字号:
/***********************************************
 /*
 /*用户操作
 /*
 /******************************************* */
package com.admin;

import java.sql.*;
import java.util.ArrayList;

import com.util.*;

public class Admin {
	
	  public String adminid = "";
	  
	  public String pwd="";
	  
	  public String prelogin="";
	  
	  public String logintime="";
	  
	  public int totle=0;
	  
	  public int id;

	  
     public Admin() {
            	 
	 }
  
  //管理员登陆
 	public int login() throws Exception{
	    Connection conn=null;
	    PreparedStatement stmt =null;
	    ResultSet rs =null;
	    int flag=0;
	      String sql = "select * from admin where adminid=?";
	    try{
	      conn =DBConn.getConn();
	      stmt =conn.prepareStatement(sql); 
	      stmt.setString(1, adminid);
	      rs = stmt.executeQuery();
	      if(rs.next()){
	    	  sql="select * from admin where adminid=? and pwd=?";
	    	  stmt =conn.prepareStatement(sql);
		      stmt.setString(1, adminid);
		      stmt.setString(2, pwd);
		      rs = stmt.executeQuery();
		       if(rs.next()){
		    	   
		     	  java.util.Date date=new java.util.Date();
				   Timestamp tt=new Timestamp(date.getTime());  
				   
		    	      if(rs.getInt("totle")==0){
		    	        sql="update admin set totle=1,logintime=? where adminid=?";
			    	    stmt =conn.prepareStatement(sql);
				        stmt.setTimestamp(1, tt);
				        stmt.setString(2,adminid);
				        stmt.executeUpdate();
		    	      }else{
			    	    sql="update admin set totle=totle+1,logintime=?,prelogin=? where adminid=?";
				    	stmt =conn.prepareStatement(sql);
					    stmt.setTimestamp(1, tt);
					    stmt.setString(2,rs.getString("logintime"));
					    stmt.setString(3,adminid);
					    stmt.executeUpdate();	  
		    	      }

		    	   flag=3;//登陆成功
		       }
		       else{
		    	   flag=2;//密码错误
		       }
	      }else{
	        flag=1;//不存在此管理员
	      }
	    }catch(Exception e){
	    	e.printStackTrace();   
	    }finally{
	      try{
	        if(rs!=null)
	          rs.close();
	        if(stmt!=null)
	          stmt.close();
	        if(conn!=null)
	          conn.close();
	      }catch(Exception ee){
	    	  ee.printStackTrace();   
	      }
	    }
	   return flag;
	  }

 	
    //判断是否存在该管理员名称
 	public int isAdminExist() throws Exception{
	    Connection conn=null;
	    PreparedStatement stmt =null;
	    ResultSet rs =null;
	    int flag=0;
	      String sql = "select * from admin where adminid=?";
	    try{
	      conn =DBConn.getConn();
	      stmt =conn.prepareStatement(sql); 
	      stmt.setString(1, adminid);
	      rs = stmt.executeQuery();
	      if(rs.next()){
	    	   flag=1;//存在
		       }
		   else{
		    	   flag=2;//不存在
		       }
	      
	    }catch(Exception e){
	    	e.printStackTrace();   
	    }finally{
	      try{
	        if(rs!=null)
	          rs.close();
	        if(stmt!=null)
	          stmt.close();
	        if(conn!=null)
	          conn.close();
	      }catch(Exception ee){
	    	  ee.printStackTrace();   
	      }
	    }
	   return flag;
	  }

 	
// 	添加管理员	
	 public Boolean add() throws Exception{
		  Connection conn=null;
		  PreparedStatement stmt =null;
		  ResultSet rs =null;
		  String sql ="insert into admin (adminid,pwd) values (?,?)";
	      boolean result=false;
	      
	      try{
			   
	    	 conn=DBConn.getConn();
	    	 stmt= conn.prepareStatement(sql); 
	    	 stmt.setString(1,this.adminid);
	    	 stmt.setString(2,this.pwd);
	    	 
	    	 stmt.executeUpdate(); 
	    	  
	    		 result=true; 
	    	
	    	}
	      catch(Exception e){
	    		e.printStackTrace(); 
	    	}
	    	finally{
	    	      try{
	    	        if(rs!=null)
	    	          rs.close();
	    	        if(stmt!=null)
	    	          stmt.close();
	    	        if(conn!=null)
	    	          conn.close();
	    	      }catch(Exception e){
	    	    	  e.printStackTrace(); 
	    	      }
	    	    }
	       return result;    	 
	  } 

	   
   
	 //查询管理员信息  
	    public ArrayList AllAdmin() throws Exception{
	  	  Connection conn=null;
	  	  PreparedStatement stmt =null;
	  	  ResultSet rs =null;
	  	  String sql ="select * from admin ";
	        ArrayList<Admin> result=new ArrayList<Admin>();
	        
	        try{
	      	 conn=DBConn.getConn();
	      	 stmt= conn.prepareStatement(sql); 
	      	 rs = stmt.executeQuery();    	 
	      	     	  
	      	  while (rs.next()){
	      		  Admin aa=new Admin();
	      		  aa.adminid=rs.getString("adminid");
	      		  aa.pwd=rs.getString("pwd");
	      		  aa.prelogin=rs.getString("prelogin");
	      		  if (rs.getString("logintime")!=null){
	      		     aa.logintime=rs.getString("logintime").substring(0,19);
	      		  }else{
	      			 aa.logintime="<font color=red>从未登陆过</font>";
	      		  }
	      		  aa.totle=rs.getInt("totle");	
	      		  aa.id=rs.getInt("id");

	      		  result.add(aa);
	      		  
	      	  }
	      	
	      	}
	        catch(Exception e){
	      		e.printStackTrace(); 
	      	}
	      	finally{
	      	      try{
	      	        if(rs!=null)
	      	          rs.close();
	      	        if(stmt!=null)
	      	          stmt.close();
	      	        if(conn!=null)
	      	          conn.close();
	      	      }catch(Exception e){
	      	    	  e.printStackTrace(); 
	      	      }
	      	    }
	         return result;    	 
	    }
	 
//	 	获取管理员信息
	 	  public Boolean getAdminInfo() throws Exception{
	 		  Connection conn=null;
	 		  PreparedStatement stmt =null;
	 		  ResultSet rs =null;
	 		  String sql ="select * from admin where adminid=?";
	 	      boolean result=false;
	 	      
	 	      try{
	 	    	 conn=DBConn.getConn();
	 	    	 stmt= conn.prepareStatement(sql); 
	 	    	 stmt.setString(1,adminid);     	
	 	    	 rs = stmt.executeQuery();    	 
	 	    	     	  
	 	    	  if (rs.next()){
	 				 
	 	    		  totle=rs.getInt("totle");
	 	    		  logintime=rs.getString("logintime");
	 	    		  prelogin=rs.getString("prelogin");
	 
	 	    		 result=true; 
	 	    	  }
	 	    	
	 	    	}
	 	      catch(Exception e){
	 	    		e.printStackTrace(); 
	 	    	}
	 	    	finally{
	 	    	      try{
	 	    	        if(rs!=null)
	 	    	          rs.close();
	 	    	        if(stmt!=null)
	 	    	          stmt.close();
	 	    	        if(conn!=null)
	 	    	          conn.close();
	 	    	      }catch(Exception e){
	 	    	    	  e.printStackTrace(); 
	 	    	      }
	 	    	    }
	 	       return result;    	 
	 	  } 
	 	  
	 	  
	 	  
//	 	删除管理员	
	 	 public Boolean delAdmin(int a_id) throws Exception{
	 		  Connection conn=null;
	 		  PreparedStatement stmt =null;
	 		  ResultSet rs =null;
	 		  String sql ="delete from admin where id=?";
	 	      boolean result=false;
	 	      
	 	      try{
	 			   
	 	    	 conn=DBConn.getConn();
	 	    	 stmt= conn.prepareStatement(sql); 
	 	    	 stmt.setInt(1,a_id);
	 	    		 	    	 
	 	    	 stmt.executeUpdate(); 
	 	    	  
	 	    		 result=true; 
	 	    	
	 	    	}
	 	      catch(Exception e){
	 	    		e.printStackTrace(); 
	 	    	}
	 	    	finally{
	 	    	      try{
	 	    	        if(rs!=null)
	 	    	          rs.close();
	 	    	        if(stmt!=null)
	 	    	          stmt.close();
	 	    	        if(conn!=null)
	 	    	          conn.close();
	 	    	      }catch(Exception e){
	 	    	    	  e.printStackTrace(); 
	 	    	      }
	 	    	    }
	 	       return result;    	 
	 	  } 	 	  
	
	 	 
//密码修改	
	 	 public Boolean changePwd() throws Exception{
	 		  Connection conn=null;
	 		  PreparedStatement stmt =null;
	 		  ResultSet rs =null;
	 		  String sql ="update admin set pwd=? where adminid=?";
	 	      boolean result=false;
	 	      
	 	      try{
	 			   
	 	    	 conn=DBConn.getConn();
	 	    	 stmt= conn.prepareStatement(sql); 
	 	    	 stmt.setString(1,pwd);
	 	    	 stmt.setString(2,adminid);
	 	    		 	    	 
	 	    	 stmt.executeUpdate(); 
	 	    	  
	 	    		 result=true; 
	 	    	
	 	    	}
	 	      catch(Exception e){
	 	    		e.printStackTrace(); 
	 	    	}
	 	    	finally{
	 	    	      try{
	 	    	        if(rs!=null)
	 	    	          rs.close();
	 	    	        if(stmt!=null)
	 	    	          stmt.close();
	 	    	        if(conn!=null)
	 	    	          conn.close();
	 	    	      }catch(Exception e){
	 	    	    	  e.printStackTrace(); 
	 	    	      }
	 	    	    }
	 	       return result;    	 
	 	  } 	 	 
	 	 
}

⌨️ 快捷键说明

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