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

📄 login.java~20~

📁 用户登录
💻 JAVA~20~
字号:
package myapp;
import java.sql.*;
import javax.servlet.http.HttpSession;
/**
 * <p>用户登录管理类</p>
 */
public class Login {
   private String username; //登录用户名称
   private String loginIP; //用户IP
   private String rights; //用户权限
   private String loginDateTime; //用户登陆日期时间
   private Object session;
   public Login() {
      username = "";
      loginIP = "";
      loginDateTime = "";
   }
   public String getUsername() {
      return username;
   }
   public void setUsername(String user) {
      username = user;
   }
   public String getRights() {
      return rights;
   }
   public void setRights(String userRights) {
      rights = userRights;
   }
   public String getLoginIP() {
      return loginIP;
   }
   public void setLoginIP(String userIP) {
      loginIP = userIP;
   }
   public String getLoginDateTime() {
      return loginDateTime;
   }
   public void setLoginDateTime(String currentDateTime) {
      loginDateTime = currentDateTime;
   }

   //检查用户是否存在
   public boolean userExist(String username) {
      Connection con = null;
      Statement stmt = null;
      PreparedStatement ps = null;
      ResultSet rs = null;
      boolean exist = false;
      String sql = "select * from admin where username=?"; //构造查询sql语句
      DBConnection dbConnection = new DBConnection();
      dbConnection.getConnect();
      try {
        con = dbConnection.con;
        ps = con.prepareStatement(sql);
        ps.setString(1, username);
        rs = ps.executeQuery();
        if (!rs.next()) {
          exist = true;
        }
      }
      catch (SQLException e) {
        e.printStackTrace();
      }
      finally {
        dbConnection.closeConn();
      }
      return exist;
    }
    //判断用户是否合法
    public boolean isValidUser(String username, String password) {
       Connection con = null;
       Statement stmt = null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       boolean isValid = false;
       String sql = "select * from admin where username=? and password=?"; //构造查询sql语句
       DBConnection dbConnection = new DBConnection();
       dbConnection.getConnect();
       try {
         con = dbConnection.con;
         ps = con.prepareStatement(sql);
         ps.setString(1, username);
         ps.setString(2, password);
         rs = ps.executeQuery();
         if (rs.next()) {
           isValid = true;
         }
       }
       catch (SQLException e) {
         e.printStackTrace();
       }
       finally {
         dbConnection.closeConn();
       }
       return isValid;
    }
    //获取用户编号ID
    public int userID(String username, String password) {
       Connection con = null;
       Statement stmt = null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       int id = 0;
       String sql = "select id from admin where username=? and password=?"; //构造查询sql语句
       DBConnection dbConnection = new DBConnection();
       dbConnection.getConnect();
       try {
         con = dbConnection.con;
         ps = con.prepareStatement(sql);
         ps.setString(1, username);
         ps.setString(2, password);
         rs = ps.executeQuery();
         if (rs.next()) {
           id = rs.getInt("id");
         }
       }
       catch (SQLException e) {
         e.printStackTrace();
       }
       finally {
         dbConnection.closeConn();
       }
       return id;
    }
    //获取用户权限
    public String userRights(String username, String password) {
       Connection con = null;
       Statement stmt = null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       String rights = null;
       String sql = "select rights from admin where username=? and password=?"; //构造查询sql语句
       DBConnection dbConnection = new DBConnection();
       dbConnection.getConnect();
       try {
         con = dbConnection.con;
         ps = con.prepareStatement(sql);
         ps.setString(1, username);
         ps.setString(2, password);
         rs = ps.executeQuery();
         if (rs.next()) {
           rights = rs.getString("rights");
         }
       }
       catch (SQLException e) {
         e.printStackTrace();
       }
       finally {
         dbConnection.closeConn();
       }
       return rights;
    }
    //修改用户登陆信息
    public void setUserLoginInfo(String currentDateTime, String userIP, String username) {
       Connection con = null;
       Statement stmt = null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       String sql = "update admin set loginDateTime = ? , loginIP= ? where username = ?"; //构造更新sql语句
       DBConnection dbConnection = new DBConnection();
       dbConnection.getConnect();
       try {
         con = dbConnection.con;
         ps = con.prepareStatement(sql);
         ps.setString(1, currentDateTime);
         ps.setString(2, userIP);
         ps.setString(3, username);
         ps.executeUpdate();
      }
      catch (SQLException e) {
        e.printStackTrace();
      }
      finally {
        dbConnection.closeConn();
      }
    }
    //记录用户登陆日志//修改用户登陆信息
    public void saveLoginInfo(String currentDateTime, String userIP, String username) {
       Connection con = null;
       Statement stmt = null;
       PreparedStatement ps = null;
       ResultSet rs = null;
       String sql = "update admin set loginDateTime = ? , loginIP= ? where username = ?"; //构造更新sql语句
       DBConnection dbConnection = new DBConnection();
       dbConnection.getConnect();
       try {
         con = dbConnection.con;
         ps = con.prepareStatement(sql);
         ps.setString(1, currentDateTime);
         ps.setString(2, userIP);
         ps.setString(3, username);
         ps.executeUpdate();
      }
      catch (SQLException e) {
        e.printStackTrace();
      }
      finally {
        dbConnection.closeConn();
      }
    }

}

⌨️ 快捷键说明

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