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

📄 user.java

📁 用户登录
💻 JAVA
字号:
package myapp;

import java.sql.*;

/**
 * <p>用户登录管理类</p>
 */
public class User {
  private String username; //登录用户名称
  private String loginIP; //用户IP

  private String loginDateTime; //用户登录日期时间
  public User() {
    username = "";
    loginIP = "";
    loginDateTime = "";
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String user) {
    username = user;
  }

  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 userLogin where username=?"; //构造查询sql语句
    DBConnection dbConnection = new DBConnection();
    dbConnection.getConnection();
    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 userLogin where username=? and password=?"; //构造查询sql语句
    DBConnection dbConnection = new DBConnection();
    dbConnection.getConnection();
    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 userLogin where username=? and password=?"; //构造查询sql语句
    DBConnection dbConnection = new DBConnection();
    dbConnection.getConnection();
    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 userLogin where username=? and password=?"; //构造查询sql语句
    DBConnection dbConnection = new DBConnection();
    dbConnection.getConnection();
    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 userLogin set loginDateTime = ? ,loginIP= ? where username = ?"; //构造更新sql语句
    DBConnection dbConnection = new DBConnection();
    dbConnection.getConnection();
    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 saveUserLoginLog(String username, String loginDateTime) {
    Connection con = null;
    Statement stmt = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql = "insert into userLoginLog(username,loginDateTime) values(?,?)"; //构造插入sql语句
    DBConnection dbConnection = new DBConnection();
    dbConnection.getConnection();
    try {
      con = dbConnection.con;
      ps = con.prepareStatement(sql);
      ps.setString(1, username);
      ps.setString(2, loginDateTime);
      ps.executeUpdate();
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
    finally {
      dbConnection.closeConn();
    }
  }

  //记录用户登录日志(退出)
  public void saveUserLoginOutLog(String loginOutDateTime, String username) {
    Connection con = null;
    Statement stmt = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    String sql =
        "update userLoginOut set loginOutDateTime = ? ,where username = ?"; //构造更新sql语句
    DBConnection dbConnection = new DBConnection();
    dbConnection.getConnection();
    try {
      con = dbConnection.con;
      ps = con.prepareStatement(sql);
      ps.setString(1, loginOutDateTime);
      ps.setString(2, username);
      ps.executeUpdate();
    }
    catch (SQLException e) {
      e.printStackTrace();
    }
    finally {
      dbConnection.closeConn();
    }
  }

  //获取当前日期时间
  public String getCurrentDateTime() {
    String loginDateTime = null;
    java.util.Date DateTime = new java.util.Date(); //设置日期时间格式
    java.text.SimpleDateFormat currentDateTime = new java.text.SimpleDateFormat(
        "yyyy-MM-dd hh:mm:ss");
    loginDateTime = currentDateTime.format(DateTime); //获取当前日期时间
    return loginDateTime;
  }
}

⌨️ 快捷键说明

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