📄 userlogin.java~50~
字号:
package myapp;
import java.sql.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* <p>用户登录管理类</p>
*/
public class userLogin {
//Connection con = null;
//Statement stmt = null;
//PreparedStatement ps = null;
//ResultSet rs = null;
private String username; //登录用户名称
private String loginIP; //用户IP
private String loginDateTime; //用户登陆日期时间
private Object session;
public userLogin() {
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 admin where username=?"; //构造查询sql语句
DBconn dbconn = new DBconn();
dbconn.getConnect();
try {
con = dbconn.con;
ps = con.prepareStatement(sql);
ps.setString(1, username);
rs = ps.executeQuery();
if (!rs.next()) {
exist = true;
}
}
catch (SQLException e) {
e.printStackTrace();
}
finally {
dbconn.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语句
DBconn dbconn = new DBconn();
dbconn.getConnect();
try {
con = dbconn.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 {
dbconn.closeConn();
}
return isValid;
}
//判断用户是否登陆
//public boolean isUserLogin(String username) {
public boolean isUserLogin(HttpServletRequest req,HttpServletResponse resp,String username) {
boolean isLogin = true;
String userLoginName = (String)httpsesession.getAttribute("username");
if (userLoginName == null){
isLogin = false;
}
return isLogin;
}
//判断用户是否合法
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语句
DBconn dbconn = new DBconn();
dbconn.getConnect();
try {
con = dbconn.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 {
dbconn.closeConn();
}
return isValid;
}
//判断用户是否合法
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语句
DBconn dbconn = new DBconn();
dbconn.getConnect();
try {
con = dbconn.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 {
dbconn.closeConn();
}
return isValid;
}
//判断用户是否合法
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语句
DBconn dbconn = new DBconn();
dbconn.getConnect();
try {
con = dbconn.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 {
dbconn.closeConn();
}
return isValid;
}
//判断用户是否合法
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语句
DBconn dbconn = new DBconn();
dbconn.getConnect();
try {
con = dbconn.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 {
dbconn.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语句
DBconn dbconn = new DBconn();
dbconn.getConnect();
try {
con = dbconn.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 {
dbconn.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语句
DBconn dbconn = new DBconn();
dbconn.getConnect();
try {
con = dbconn.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 {
dbconn.closeConn();
}
return rights;
}
//记录用户登陆日志
public void setUserInformation(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语句
DBconn dbconn = new DBconn();
dbconn.getConnect();
try {
con = dbconn.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 {
dbconn.closeConn();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -