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

📄 users.java

📁 使用J2EE Struts开发的房地产信息咨询系统
💻 JAVA
字号:
/**
 * 注册用户的Java Bean
 * Users.java
 * @author usr
 */

package building;

import java.util.*;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Users 
{
	/**
	 * 基本属性
	 */
	private int id = 0; //注册用户ID
	private String userName = null; //注册用户登录名
	private String trueName = null; //注册用户真实姓名
	private String password = null; //登录密码
	private String pswNotice = null; //密码问题提示
	private String pswItIs = null; //密码问题提示答案
	private String sex = null; //性别
	private String birth = null; //出生年月
	private String tel = null; //联系电话
	private String email = null; //电子邮件地址
	private String address = null; //联系地址 
	private String lastLoginTime = "2006-5-12"; //最后登录时间
	
	/**
	 * 构造方法
	 */
	public Users(){}
	
	/**
	 * 基本属性的set及get方法
	 */
	//注册用户ID的set及get方法
	public void setId(int id)
	{
		this.id = id;
	}//End of setId
	
	public int getId()
	{
		return(id);
	}//End of getId
	
	//注册用户真实姓名的set及get方法
	public void setUserName(String userName)
	{
		this.userName = userName;
	}//End of setUserName
	
	public String getUserName()
	{
		return(userName);
	}//End of getUserName
	
	
	//注册用户真实姓名的set及get方法
	public void setTrueName(String trueName)
	{
		this.trueName = trueName;
	}//End of setTrueName
	
	public String getTrueName()
	{
		return(trueName);
	}//End of trueName
	
	//登录密码的set及get方法
	public void setPassword(String password)
	{
		this.password = password;
	}//End of setPassword
	
	public String getPassword()
	{
		return(password);
	}//End of getPassword
	
	
	//密码问题提示的set及get方法
	public void setPswNotice(String pswNotice)
	{
		this.pswNotice = pswNotice;
	}//End of setPswNotice
	
	public String getPswNotice()
	{
		return(pswNotice);
	}//End of getPswNotice
	
	
	//密码问题提示答案的set及get方法
	public void setPswItIs(String pswItIs)
	{
		this.pswItIs = pswItIs;
	}//End of setPswItIs
	
	public String getPswItIs()
	{
		return(pswItIs);
	}//End of getPswItIs
	
	
	//性别的set及get方法
	public void setSex(String sex)
	{
		this.sex = sex;
	}//End of setSex
	
	public String getSex()
	{
		return(sex);
	}//End of getSex
	
	
	//出生年月的set及get方法
	public void setBirth(String birth)
	{
		this.birth = birth;
	}//End of setBirth
	
	public String getBirth()
	{
		return(birth);
	}//End of getBirth
	
	
	//联系电话的set及get方法
	public void setTel(String tel)
	{
		this.tel = tel;
	}//End of setTel
	
	public String getTel()
	{
		return(tel);
	}//End of getTel
	
	
	//电子邮件地址的set及get方法
	public void setEmail(String email)
	{
		this.email = email;
	}//End of setEmail
	
	public String getEmail()
	{
		return(email);
	}//End of getEmail
	
	
	//联系地址的set及get方法
	public void setAddress(String address)
	{
		this.address = address;
	}//End of setAddress
	
	public String getAddress()
	{
		return(address);
	}//End of getAddress
	
	
	//最后登录时间的set及get方法
	public void setLastLoginTime(String lastLoginTime)
	{
		this.lastLoginTime = lastLoginTime;
	}//End of setLastLoginTime
	
	public String getLastLoginTime()
	{
		return(lastLoginTime);
	}//End of getLastLoginTime
	
	
	/**
	 * 数据库操作方法
	 */
	
	/**
	 * 检查用户名是否已经注册
	 */
	public static boolean ifExist(DB db,String userName)
	throws Exception
	{
		String strSql = null;
		ResultSet rs = null;
		
		strSql = "select * from Users where username='" + userName + "'";
		rs = db.OpenSql(strSql);
		
		if (rs.next())
		{
			//用户存在,验证成功
			return(true);
		}//End of if
		else
		{
			//拥护不存在,验证失败
			return(false);
		}//End of else
	}//End of ifExist
	
	/**
	 * 注册用户登录验证
	 */
	public static boolean checkUser(DB db,String userName,String password)
	throws SQLException
	{
		String strSql = null;
		ResultSet rs = null;
		
		strSql = "select * from Users where username='" + userName
			 + "' and password='" + password + "'";
		rs = db.OpenSql(strSql);
		
		if (rs.next())
		{
			//用户存在,验证成功
			return(true);
		}//End of if
		else
		{
			//拥护不存在,验证失败
			return(false);
		}//End of else
	}//End of checkUser
	
	
	/**
	 * 查询用户的所有信息
	 */
	public static Users userInfo(DB db,String userName)
	throws SQLException
	{
		String strSql = null;
		ResultSet rs = null;
		
		strSql = "select * from Users where username='" + userName + "'";
		rs = db.OpenSql(strSql);
		
		if (rs.next())
		{
			//查询到用户信息
			Users user = new Users();
			
			user.setId(rs.getInt("id"));
			user.setUserName(rs.getString("username"));
			user.setTrueName(rs.getString("truename"));
			user.setSex(rs.getString("sex"));
			user.setBirth(rs.getString("birth"));
			user.setTel(rs.getString("tel"));
			user.setEmail(rs.getString("email"));
			user.setAddress(rs.getString("address"));
			
			return(user);
		}//End of if
		
		else
		{
			//没有查询到任何信息
			return(null);
		}//End of else
	}//End of userInfo
	
	/**
	 * 查询用户的所有信息
	 */
	public static Users userInfo(DB db,int userId)
	throws SQLException
	{
		String strSql = null;
		ResultSet rs = null;
		
		strSql = "select * from Users where id='" + userId + "'";
		rs = db.OpenSql(strSql);
		
		if (rs.next())
		{
			//查询到用户信息
			Users user = new Users();
			
			user.setId(userId);
			user.setUserName(rs.getString("username"));
			user.setTrueName(rs.getString("truename"));
			user.setSex(rs.getString("sex"));
			user.setBirth(rs.getString("birth"));
			user.setTel(rs.getString("tel"));
			user.setEmail(rs.getString("email"));
			user.setAddress(rs.getString("address"));
			
			return(user);
		}//End of if
		
		else
		{
			//没有查询到任何信息
			return(null);
		}//End of else
	}//End of userInfo
	
	/**
	 * 根据用户ID查询用户的姓名
	 */
	public static String userName(DB db,int userId)
	throws SQLException
	{
		String strSql = null;
		ResultSet rs = null;
		
		strSql = "select username from Users where id='" + userId + "'";
		rs = db.OpenSql(strSql);
		
		if (rs.next())
		{
			//查询到用户信息
			Users user = new Users();
						
			return(rs.getString("username"));
		}//End of if
		
		else
		{
			//没有查询到任何信息
			return(null);
		}//End of else
	}//End of userInfo
	
	
	/**
	 * 添加新注册用户信息
	 */
	public boolean addUser(DB db)
	throws SQLException
	{
		String strSql = null;
		ResultSet rs = null;
		int iMax = 0;
		
		//查找最大的ID号
		strSql = "select Max(id) from Users";
		rs = db.OpenSql(strSql);
		
		if (rs.next())
		{
			iMax = rs.getInt(1);
			iMax++;
		}//End of if
		else
		{
			iMax = 1;
		}//End of else
		
		strSql = "insert into Users values("
			+ iMax + ",'"
			+ userName + "','"
			+ trueName + "','"
			+ password + "','"
			+ pswNotice + "','"
			+ pswItIs + "','"
			+ sex + "','"
			+ birth + "','"
			+ tel + "','"
			+ email + "','"
			+ address + "','"
			+ lastLoginTime + "')";
		
		if ( db.ExecSql(strSql) == 0 )
		{
			//添加失败
			return(false);
		}//End of if
		else
		{
			//添加成功
			return(true);
		}//End of else
	}//End of addUser
	
	
	//用户修改自身的 资料
	public boolean updateInfo(DB db)
	throws Exception
	{
		String strSql = null;
				
		strSql = "update Users set "
			+ "truename='" + trueName
			+ "',sex='" + sex
			+ "',birth='" + birth
			+ "',tel='" + tel
			+ "',address='" + address
			+ "',email='" + email
			+ "' where username='" + userName
			+ "'";
		
		if ( db.ExecSql(strSql) ==0 )
		{
			//修改失败
			return(false);
		}//End of if
		else
		{
			//修改成功
			return(true);
		}//End of else
	}//End of updateInfo
	
	
	/**
	 * 修改用户密码的方法
	 * @author usr
	 */
	public static boolean updatePsw(DB db,String newPsw,String userName)
	throws Exception
	{
		String strSql = null;
		
		strSql = "update Users set "
			+ "password='" + newPsw
			+ "' where username='" + userName
			+ "'";
		
		if ( db.ExecSql(strSql) == 0 )
		{
			//更新失败
			return(false);
		}//End of if
		else
		{
			//更新成功
			return(true);
		}//End of else
		
	}//End of updatePsw
	
	/**
	 * 获取所有用户的信息
	 */
	public static Vector getAllInfo(DB db)
	throws SQLException
	{
		String strSql = null;
		ResultSet rs = null;
		Vector userList = new Vector();
		
		strSql = "exec SELECT_USER_ALL";//存储过程的应用
		rs = db.OpenSql(strSql);
		
		while (rs.next())
		{
			Users user = new Users();
			
			user.setId(rs.getInt("id"));
			user.setUserName(rs.getString("username"));
			user.setTrueName(rs.getString("truename"));
			user.setPassword("");
			user.setPswNotice(rs.getString("pswnotice"));
			user.setPswItIs(rs.getString("pswitis"));
			user.setSex(rs.getString("sex"));
			user.setTel(rs.getString("tel"));
			user.setEmail(rs.getString("email"));
			user.setAddress(rs.getString("address"));
			user.setLastLoginTime(rs.getString("lastlogintime"));
			
			userList.add(user);
		}//End of while
		
		return(userList);
	}//End of getAllInfo
	
	/**
	 * 删除注册用户
	 */
	public static boolean delUser(DB db, int userId)
	throws SQLException
	{
		String strSql = null;
		
		strSql = "delete from Users where id=" + userId;
		
		if ( db.ExecSql(strSql) == 0 )
		{
			//删除操作失败
			return(false);
		}//End of if
		else
		{
			//删除操作成功
			return(true);
		}//End of esle
	}//End of delUser
	
}//End of class User

⌨️ 快捷键说明

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