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

📄 profile.java

📁 简单的用户注册系统 解压
💻 JAVA
字号:
package com.jdon.simpleregister;

import java.util.*;
import java.sql.*;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;

/**
 *
 * 本程序为帮助初学者了解Jsp/javabean 不建议商业用途 进一步使用请参考Struts框架
 *
 * J道版权所有 
 * J道(http://www.jdon.com) Java的解决之道。
 *
 */

public class Profile {
  //用户登陆Id
  private String userid = "";
  public void setUserid(String userid) {
    this.userid = userid;
  }
  public String getUserid() {
    return userid;
  }
  //用户密码
  private String password = null;
  public void setPassword(String password) {
    this.password = password;
  }
  public String getPassword() {
    return password;
  }
  //用户姓名
  private String username = "";
  public void setUsername(String username) {
    this.username = username;
  }
  public String getUsername() {
    return username;
  }
  //用户Email
  private String email = "";
  public void setEmail(String email) {
    this.email = email;
  }
  public String getEmail() {
    return email;
  }
  //用户性别
  private int gender = 0;
  public void setGender(int gender) {
    this.gender = gender;
  }
  public int getGender() {
    return gender;
  }
  //用户职业
  private int occupation = 0;
  public void setOccupation(int occupation) {
    this.occupation = occupation;
  }
  public int getOccupation() {
    return occupation;
  }
  //用户住址
  private String location = "";
  public void setLocation(String location) {
    this.location = location;
  }
  public String getLocation() {
    return location;
  }
  //用户城市
  private String city = "";
  public void setCity(String city) {
    this.city = city;
  }
  public String getCity() {
    return city;
  }
  //用户国家
  private int country = 0;
  public void setCountry(int country) {
    this.country = country;
  }
  public int getCountry() {
    return country;
  }
  //用户邮编
  private String zipcode = "";
  public void setZipcode(String zipcode) {
    this.zipcode = zipcode;
  }
  public String getZipcode() {
    return zipcode;
  }
  //用户家庭电话
  private String homephone = "";
  public void setHomephone(String homephone) {
    this.homephone = homephone;
  }
  public String getHomephone() {
    return homephone;
  }
  //身份证
  private String cardnumber = "";
  public void setCardnumber(String cardnumber) {
    this.cardnumber = cardnumber;
  }
  public String getCardnumber() {
    return cardnumber;
  }

  private String birthday = "";
  public String getBirthday(){
     birthday = new String(year + "-" + month + "-" + day);
     return birthday;
  }
  public void setBirthday(String birthday){
     this.birthday =   birthday;
  }

  //生日的天
  private String day = "";
  public void setDay(String day) {
    this.day = day;
  }
  public String getDay() {
    return day;
  }
  //生日的年
  private String year = "1900";
  public void setYear(String year) {
    this.year = year;
  }
  public String getYear() {
    return year;
  }
  //生日的月
  private String month = "";
  public void setMonth(String month) {
    this.month = month;
  }
  public String getMonth() {
    return month;
  }
  //注册时的IP地址
  private String regip = "";
  public void setRegip(String regip) {
    this.regip = regip;
  }
  public String getRegip() {
    return regip;
  }
  //密码提示回答
  private String passwdanswer = "";
  public void setPasswdanswer(String passwdanswer) {
    this.passwdanswer = passwdanswer;
  }
  public String getPasswdanswer() {
    return passwdanswer;
  }
  //密码提示问题的类型
  private int passwdtype = 0;
  public void setPasswdtype(int passwdtype) {
    this.passwdtype = passwdtype;
  }
  public int getPasswdtype() {
    return passwdtype;
  }

  /**
   * 插入个人资料
   * @return
   * @throws java.lang.Exception
   */
  public int insert() throws Exception {
    if (!isValid())
      return Constants.FORM_ERROR;
    if (EmailExist())
      return Constants.FORM_ERROR;
    String birthday = new String(year + "-" + month + "-" + day);

    String insertsql =
        "insert into profile values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    String passwordsql =
        "insert into password (userid,password) values (?,PASSWORD(?))";
    String passwordassitsql =
        "insert into passwordassit (userid,oldpassword,passwdtype,passwdanswer) values (?,?,?,?)";

    Mysql mysql = new Mysql(insertsql);
    try {

      mysql.setString(1, this.userid);
      mysql.setString(2, username);
      mysql.setString(3, this.email);
      mysql.setInt(4, this.gender);
      mysql.setInt(5, this.occupation);
      mysql.setString(6, this.location);
      mysql.setString(7, this.city);
      mysql.setInt(8, this.country);
      mysql.setString(9, this.zipcode);
      mysql.setString(10, this.homephone);
      mysql.setString(11, this.cardnumber);
      mysql.setString(12, birthday);
      mysql.setString(13, this.regip);

      java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
          Constants.entimeformat);
      String regdate = formatter.format(new java.util.Date());
      mysql.setString(14, regdate);
      mysql.executeUpdate();

      mysql.prepareStatement(passwordsql);
      mysql.setString(1, userid);
      mysql.setString(2, password);
      mysql.executeUpdate();

      mysql.prepareStatement(passwordassitsql);
      mysql.setString(1, userid);
      mysql.setString(2, password);
      mysql.setInt(3, this.passwdtype);
      mysql.setString(4, this.passwdanswer);
      mysql.executeUpdate();

    } catch (Exception ex) {
      throw new Exception("Profile.insert()" + ex.getMessage());

    } finally {
      mysql.close();
      mysql = null;
    }
    return Constants.OK;
  }

  /**
   * 修改个人资料
   * @return
   * @throws java.lang.Exception
   */
  public int update() throws Exception {
    if (!isValid())
      return Constants.FORM_ERROR;
    String birthday = new String(year + "-" + month + "-" + day);
    char current = ' ';
    String updatesql = "update profile set " +
        "username=?,email=?,gender=?,occupation=?,location=?,city=?,country=?," +
        "zipcode=?,homephone=?,cardnumber=?,birthday=? where userid=?";
    Mysql mysql = new Mysql(updatesql);
    try {

      mysql.setString(1, username);
      mysql.setString(2, this.email);

      mysql.setInt(3, this.gender);
      mysql.setInt(4, this.occupation);

      mysql.setString(5, this.location);
      mysql.setString(6, this.city);
      mysql.setInt(7, this.country);

      mysql.setString(8, this.zipcode);
      mysql.setString(9, this.homephone);

      mysql.setString(10, this.cardnumber);
      mysql.setString(11, birthday);

      mysql.setString(12, this.userid);
      mysql.executeUpdate();
    } catch (Exception ex) {
      throw new Exception("Profile.update()" + ex.getMessage());
    } finally {
      mysql.close();
      mysql = null;
    }
    return Constants.OK;
  }

  public void updateHBM() throws Exception {


    Session session = HibernateUtil.openSession();
    Transaction transaction;
    try {
      transaction = session.beginTransaction();

      session.update(this);
      transaction.commit();

    } catch (Exception he) {
      throw new Exception(he);
    } finally {
      HibernateUtil.closeSession(session);
    }

  }

  /**
   * 查询个人资料
   * @return
   * @throws java.lang.Exception
   */
  public boolean select() throws Exception {
    String selectsql = "select *, year(birthday) as year,month(birthday) as month,dayofmonth(birthday) as day from profile where userid = ?";
    Mysql mysql = new Mysql(selectsql);
    try {
      mysql.setString(1, userid);
      ResultSet rs = mysql.executeQuery();
      boolean next = rs.next();
      if (next) {
        username = rs.getString("username");
        email = rs.getString("email");

        gender = rs.getInt("gender");
        occupation = rs.getInt("occupation");

        location = rs.getString("location");
        city = rs.getString("city");
        country = rs.getInt("country");

        zipcode = rs.getString("zipcode");
        homephone = rs.getString("homephone");

        cardnumber = rs.getString("cardnumber");
        year = rs.getString("year");
        month = rs.getString("month");
        day = rs.getString("day");

      }
      rs.close();
      rs = null;
      return next;
    } catch (Exception ex) {
      throw new Exception("Profile.update()" + ex.getMessage());
    } finally {
      mysql.close();
      mysql = null;
    }
  }

  public boolean isValid() {
    boolean valid = true;
    if (userid == null || userid.equals(""))
      valid = false;
    if (username == null || username.equals(""))
      valid = false;

    return valid;
  }

  /**
   * 检查Email是否存在
   * @return
   * @throws java.lang.Exception
   */
  public boolean EmailExist() throws Exception {
    boolean exist = false;
    String sql = "select * from profile where email = ?";
    Mysql mysql = new Mysql(sql);
    try {
      mysql.setString(1, email);
      ResultSet rs = mysql.executeQuery();
      if (rs.next()) {
        exist = true;
      }
      rs.close();
      rs = null;
    } catch (Exception ex) {
      throw new Exception("Profile.update()" + ex.getMessage());
    } finally {
      mysql.close();
      mysql = null;
    }
    return exist;
  }

}

⌨️ 快捷键说明

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