?? profile.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 + -