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

📄 userinfo.java

📁 计算机技术的快速发展
💻 JAVA
字号:
package com.suninformation.user;

import com.suninformation.database.*;

import java.sql.*;

/**
 * @author 刘镇
 * @version 1.0
 * 
 * 用户个人信息类
 */
public class UserInfo {

	private static final String INSERT_USERINFO = "INSERT INTO puserinfo(username,realname,birthday,email,modifytime) VALUES(?,?,?,?,?)";

	private static final String LOAD_USERINFO_BY_USERNAME = "SELECT userinfoid,username,realname,birthday,email,sex,nation,height,marriage,schoolname,schoolage,workpracename,workpraceaddress,homeaddress,contactaddress,mobiletelephone,postalcode,contactphone,jobname,pay,country,city,qq,icq,other,homepage,memo,modifytime FROM puserinfo WHERE username=?";

	private static final String SAVE_USERINFO = "UPDATE puserinfo SET realname=?,birthday=?,email=?,sex=?,nation=?,height=?,marriage=?,schoolname=?,schoolage=?,workpracename=?,workpraceaddress=?,homeaddress=?,contactaddress=?,postalcode=?,contactphone=?,mobiletelephone=?,jobname=?,pay=?,country=?,city=?,qq=?,icq=?,other=?,homepage=?,memo=?,modifytime=? WHERE username=?";

	private long userInfoId = -1;

	private String userName = null;

	private String realName = null;

	private Date birthday = null;

	private String email = null;

	private int sex = -1;

	private String nation = null;

	private int height = -1;

	private int marriage = -1;

	private String schoolName = null;

	private int schoolAge = -1;

	private String workpraceName = null;

	private String workpraceAddress = null;

	private String homeAddress = null;

	private String contactAddress = null;

	private String postalcode = null;

	private String contactPhone = null;

	private String mobileTelephone = null;

	private int jobName = -1;

	private int pay = -1;

	private int country = -1;

	private int city = -1;

	private String qq = null;

	private String icq = null;

	private String other = null;

	private String homePage = null;

	private String memo = null;

	private Date modifyTime = null;

	private boolean isChanged = false;

	public UserInfo(String userName, String realName, String email,
			java.sql.Date birthday) throws UserAlreadyExistsException,
			UnacceptableException {
		this.userName = userName;
		this.realName = realName;
		this.email = email;
		this.birthday = birthday;
		this.modifyTime = new java.sql.Date(new java.util.Date().getTime());
		insertIntoDB();
	}

	public UserInfo(String userName) throws UserNotFoundException,
			UnacceptableException {
		if (userName == null) {
			throw new UnacceptableException("您输入的参数值有问题,userName不能为null。");
		}
		this.userName = userName;
		loadFromDB();
	}

	private void loadFromDB() throws UserNotFoundException,
			UnacceptableException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBManager.getConnection();
			pstmt = conn.prepareStatement(LOAD_USERINFO_BY_USERNAME);
			pstmt.setString(1, userName);
			rs = pstmt.executeQuery();
			if (!rs.next()) {
				throw new UserNotFoundException(userName + " 的用户信息不存在。");
			}
			this.userInfoId = rs.getLong(1);
			this.userName = rs.getString(2);
			this.realName = rs.getString(3);
			this.birthday = rs.getDate(4);
			this.email = rs.getString(5);
			this.sex = rs.getInt(6);
			this.nation = rs.getString(7);
			this.height = rs.getInt(8);
			this.marriage = rs.getInt(9);
			this.schoolName = rs.getString(10);
			this.schoolAge = rs.getInt(11);
			this.workpraceName = rs.getString(12);
			this.workpraceAddress = rs.getString(13);
			this.homeAddress = rs.getString(14);
			this.contactAddress = rs.getString(15);
			this.mobileTelephone = rs.getString(16);
			this.postalcode = rs.getString(17);
			this.contactPhone = rs.getString(18);
			this.jobName = rs.getInt(19);
			this.pay = rs.getInt(20);
			this.country = rs.getInt(21);
			this.city = rs.getInt(22);
			this.qq = rs.getString(23);
			this.icq = rs.getString(24);
			this.other = rs.getString(25);
			this.homePage = rs.getString(26);
			this.memo = rs.getString(27);
			this.modifyTime = rs.getDate(28);
		} catch (SQLException sqle) {
			throw new UnacceptableException("从数据库中取用户信息数据失败。", sqle);
		} finally {
			DBManager.closeObject(conn, pstmt, rs);
		}
	}

	private void insertIntoDB() throws UserAlreadyExistsException,
			UnacceptableException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			loadFromDB();
			throw new UserAlreadyExistsException("该用户的信息已存在!");
		} catch (UserNotFoundException unfe) {
			try {
				conn = DBManager.getConnection();
				pstmt = conn.prepareStatement(INSERT_USERINFO);
				pstmt.setString(1, userName);
				pstmt.setString(2, realName);
				pstmt.setDate(3, birthday);
				pstmt.setString(4, email);
				pstmt.setDate(5, modifyTime);
				pstmt.executeUpdate();
			} catch (SQLException e) {
				throw new UnacceptableException("向数据库中写入用户信息数据失败.", e);
			} finally {
				DBManager.closeObject(conn, pstmt, rs);
			}
		}
	}

	private void saveToDB() throws UnacceptableException {
		Connection conn = null;
		try {
			conn = DBManager.getConnection();
			saveToDB(conn);
		} catch (SQLException sqle) {
			throw new UnacceptableException("保存数据出错", sqle);
		} finally {
			try {
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	private void saveToDB(Connection con) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			pstmt = con.prepareStatement(SAVE_USERINFO);
			pstmt.setString(1, realName);
			pstmt.setDate(2, birthday);
			pstmt.setString(3, email);
			pstmt.setInt(4, sex);
			pstmt.setString(5, nation);
			pstmt.setInt(6, height);
			pstmt.setInt(7, marriage);
			pstmt.setString(8, schoolName);
			pstmt.setInt(9, schoolAge);
			pstmt.setString(10, workpraceName);
			pstmt.setString(11, workpraceAddress);
			pstmt.setString(12, homeAddress);
			pstmt.setString(13, contactAddress);
			pstmt.setString(14, postalcode);
			pstmt.setString(15, contactPhone);
			pstmt.setString(16, mobileTelephone);
			pstmt.setInt(17, jobName);
			pstmt.setInt(18, pay);
			pstmt.setInt(19, country);
			pstmt.setInt(20, city);
			pstmt.setString(21, qq);
			pstmt.setString(22, icq);
			pstmt.setString(23, other);
			pstmt.setString(24, homePage);
			pstmt.setString(25, memo);
			pstmt.setDate(26, modifyTime);
			pstmt.setString(27, userName);
			pstmt.executeUpdate();
		} finally {
			try {
				pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	////////////////////////////////////////////////////////////////

	/**
	 * 获取用户个人信息ID(UserInfoId)
	 * 
	 * @return long
	 */
	public long getUserInfoId() {
		return userInfoId;
	}

	/**
	 * 获取用户真实姓名(RealName)
	 * 
	 * @return String
	 */
	public String getRealName() {
		return realName;
	}

	/**
	 * 设置用户真实姓名(RealName)
	 * 
	 * @param realName
	 *            String
	 */
	public void setRealName(String realName) {
		this.realName = realName;
		this.isChanged = true;
	}

	/**
	 * 获取用户出生日期(Brithday)
	 * 
	 * @return Date
	 */
	public Date getBirthday() {
		return birthday;
	}

	/**
	 * 设置用户出生日期(Brithday)
	 * 
	 * @param birthday
	 *            Date
	 */
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
		this.isChanged = true;
	}

	/**
	 * 获取用户的电子邮件地址(Email)
	 * 
	 * @return String
	 */
	public String getEmail() {
		return email;
	}

	/**
	 * 设置用户的电子邮件地址(Email)
	 * 
	 * @param email
	 *            String
	 */
	public void setEmail(String email) {
		this.email = email;
		this.isChanged = true;
	}

	/**
	 * 获取用户性别(Sex)
	 * 
	 * @return int
	 */
	public int getSex() {
		return sex;
	}

	/**
	 * 设置用户性别(Sex)
	 * 
	 * @param sex
	 *            int
	 */
	public void setSex(int sex) {
		this.sex = sex;
		this.isChanged = true;
	}

	/**
	 * 获取用户民族(Nation)
	 * 
	 * @return String
	 */
	public String getNation() {
		return nation;
	}

	/**
	 * 设置用户民族(Nation)
	 * 
	 * @param nation
	 *            String
	 */
	public void setNation(String nation) {
		this.nation = nation;
		this.isChanged = true;
	}

	/**
	 * 获取用户身高(Height)
	 * 
	 * @return int
	 */
	public int getHeight() {
		return height;
	}

	/**
	 * 设置用户身高(Height)
	 * 
	 * @param height
	 *            int
	 */
	public void setHeight(int height) {
		this.height = height;
		this.isChanged = true;
	}

	/**
	 * 获取用户婚姻状况(Marriage)
	 * 
	 * @return int
	 */
	public int getMarriage() {
		return marriage;
	}

	/**
	 * 设置用户婚姻状况(Marriage)
	 * 
	 * @param marriage
	 *            int
	 */
	public void setMarriage(int marriage) {
		this.marriage = marriage;
		this.isChanged = true;
	}

	/**
	 * getSchoolName
	 * 
	 * @return String
	 */
	public String getSchoolName() {
		return schoolName;
	}

	/**
	 * setSchoolName
	 * 
	 * @param schoolName
	 *            String
	 */
	public void setSchoolName(String schoolName) {
		this.schoolName = schoolName;
		this.isChanged = true;
	}

	/**
	 * getSchoolAge
	 * 
	 * @return int
	 */
	public int getSchoolAge() {
		return schoolAge;
	}

	/**
	 * setSchoolAge
	 * 
	 * @param schoolAge
	 *            int
	 */
	public void setSchoolAge(int schoolAge) {
		this.schoolAge = schoolAge;
		this.isChanged = true;
	}

	/**
	 * getWorkpraceName
	 * 
	 * @return String
	 */
	public String getWorkpraceName() {
		return workpraceName;
	}

	/**
	 * setWorkpraceName
	 * 
	 * @param workpraceName
	 *            String
	 */
	public void setWorkpraceName(String workpraceName) {
		this.workpraceName = workpraceName;
		this.isChanged = true;
	}

	/**
	 * getWorkpraceAddress
	 * 
	 * @return String
	 */
	public String getWorkpraceAddress() {
		return workpraceAddress;
	}

	/**
	 * setWorkpraceAddress
	 * 
	 * @param workpraceAddress
	 *            String
	 */
	public void setWorkpraceAddress(String workpraceAddress) {
		this.workpraceAddress = workpraceAddress;
		this.isChanged = true;
	}

	/**
	 * getHomeAddress
	 * 
	 * @return String
	 */
	public String getHomeAddress() {
		return homeAddress;
	}

	/**
	 * setHomeAddress
	 * 
	 * @param homeAddress
	 *            String
	 */
	public void setHomeAddress(String homeAddress) {
		this.homeAddress = homeAddress;
		this.isChanged = true;
	}

	/**
	 * getContactAddress
	 * 
	 * @return String
	 */
	public String getContactAddress() {
		return contactAddress;
	}

	/**
	 * setContactAddress
	 * 
	 * @param contactAddress
	 *            String
	 */
	public void setContactAddress(String contactAddress) {
		this.contactAddress = contactAddress;
		this.isChanged = true;
	}

	/**
	 * getPostalcode
	 * 
	 * @return String
	 */
	public String getPostalcode() {
		return postalcode;
	}

	/**
	 * setPostalcode
	 * 
	 * @param postalcode
	 *            String
	 */
	public void setPostalcode(String postalcode) {
		this.postalcode = postalcode;
		this.isChanged = true;
	}

	/**
	 * getContactPhone
	 * 
	 * @return String
	 */
	public String getContactPhone() {
		return contactPhone;
	}

	/**
	 * setContactPhone
	 * 
	 * @param contactPhone
	 *            String
	 */
	public void setContactPhone(String contactPhone) {
		this.contactPhone = contactPhone;
		this.isChanged = true;
	}

	/**
	 * getMobileTelephone
	 * 
	 * @return String
	 */
	public String getMobileTelephone() {
		return mobileTelephone;
	}

	/**
	 * setMobileTelephone
	 * 
	 * @param mobileTelephone
	 *            String
	 */
	public void setMobileTelephone(String mobileTelephone) {
		this.mobileTelephone = mobileTelephone;
		this.isChanged = true;
	}

	/**
	 * getJobName
	 * 
	 * @return int
	 */
	public int getJobName() {
		return jobName;
	}

	/**
	 * setJobName
	 * 
	 * @param jobName
	 *            int
	 */
	public void setJobName(int jobName) {
		this.jobName = jobName;
	}

	/**
	 * getPay
	 * 
	 * @return int
	 */
	public int getPay() {
		return pay;
	}

	/**
	 * setPay
	 * 
	 * @param pay
	 *            int
	 */
	public void setPay(int pay) {
		this.pay = pay;
		this.isChanged = true;
	}

	/**
	 * getCountry
	 * 
	 * @return int
	 */
	public int getCountry() {
		return country;
	}

	/**
	 * setCountry
	 * 
	 * @param country
	 *            int
	 */
	public void setCountry(int country) {
		this.country = country;
		this.isChanged = true;
	}

	/**
	 * getCity
	 * 
	 * @return int
	 */
	public int getCity() {
		return city;
	}

	/**
	 * setCity
	 * 
	 * @param city
	 *            int
	 */
	public void setCity(int city) {
		this.city = city;
		this.isChanged = true;
	}

	/**
	 * getQq
	 * 
	 * @return int
	 */
	public String getQq() {
		return qq;
	}

	/**
	 * setQq
	 * 
	 * @param qq
	 *            int
	 */
	public void setQq(String qq) {
		this.qq = qq;
		this.isChanged = true;
	}

	/**
	 * getIcq
	 * 
	 * @return int
	 */
	public String getIcq() {
		return icq;
	}

	/**
	 * setIcq
	 * 
	 * @param icq
	 *            int
	 */
	public void setIcq(String icq) {
		this.icq = icq;
		this.isChanged = true;
	}

	/**
	 * getOther
	 * 
	 * @return String
	 */
	public String getOther() {
		return other;
	}

	/**
	 * setOther
	 * 
	 * @param other
	 *            String
	 */
	public void setOther(String other) {
		this.other = other;
		this.isChanged = true;
	}

	/**
	 * getHomePage
	 * 
	 * @return String
	 */
	public String getHomePage() {
		return homePage;
	}

	/**
	 * setHomePage
	 * 
	 * @param homePage
	 *            String
	 */
	public void setHomePage(String homePage) {
		this.homePage = homePage;
		this.isChanged = true;
	}

	/**
	 * getMemo
	 * 
	 * @return String
	 */
	public String getMemo() {
		return memo;
	}

	/**
	 * setMemo
	 * 
	 * @param memo
	 *            String
	 */
	public void setMemo(String memo) {
		this.memo = memo;
		this.isChanged = true;
	}

	/**
	 * getModifyTime
	 * 
	 * @return Date
	 */
	public Date getModifyTime() {
		return modifyTime;
	}

	/**
	 * 保存信息修改;当对该类的属性进行更改后,可以使用该方法进行写入数据库操作。
	 *  
	 */
	public void save() {
		if (this.isChanged) {
			try {
				saveToDB();
			} catch (UnacceptableException ue) {
			}

		}
	}
}

⌨️ 快捷键说明

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