📄 profile.java
字号:
package signup;
import java.util.*;
import java.sql.*;
/**
*
* 本程序为帮助初学者了解Jsp/javabean 不建议商业用途 进一步使用请参考Struts框架
*
* J道版权所有
* J道(http://www.jdon.com) Java的解决之道。
*
*/
public class Profile {
private static BeansConstants CONST = BeansConstants.getInstance();
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;
}
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 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;
}
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;
}
private String clubid = "";
public void setClubid(String clubid) {
this.clubid = clubid;
}
public String getClubid() {
return clubid;
}
private String selectsql = "select *, year(birthday) as year,month(birthday) as month,dayofmonth(birthday) as day from profile where userid = ?";
public int insert() throws Exception {
if (!isValid())
return CONST.FORM_ERROR;
if (EmailExist())
return CONST.FORM_ERROR;
String birthday = new String(year + "-" + month + "-" + day);
try {
String insertsql =
"insert into profile values (?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
Mysql mysql = new Mysql(insertsql);
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(
CONST.entimeformat);
String regdate = formatter.format(new java.util.Date());
mysql.setString(14, regdate);
mysql.executeUpdate();
String passwordsql =
"insert into password (userid,password) values (?,PASSWORD(?))";
mysql.prepareStatement(passwordsql);
mysql.setString(1, userid);
mysql.setString(2, password);
mysql.executeUpdate();
String passwordassitsql = "insert into passwordassit (userid,oldpassword,passwdtype,passwdanswer) values (?,?,?,?)";
mysql.prepareStatement(passwordassitsql);
mysql.setString(1, userid);
mysql.setString(2, password);
mysql.setInt(3, this.passwdtype);
mysql.setString(4, this.passwdanswer);
mysql.executeUpdate();
String sql = "INSERT INTO userclub VALUES (?,?,?)";
mysql.prepareStatement(sql);
mysql.setString(1, userid);
mysql.setString(2, clubid);
mysql.setInt(3, 0);
mysql.executeUpdate();
mysql.close();
mysql = null;
}
catch (Exception ex) {
throw new Exception("Profile.insert()" + ex.getMessage());
}
return CONST.OK;
}
public int update() throws Exception {
if (!isValid())
return CONST.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=?";
try {
Mysql mysql = new Mysql(updatesql);
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();
mysql.close();
}
catch (Exception ex) {
throw new Exception("Profile.update()" + ex.getMessage());
}
return CONST.OK;
}
public boolean select() throws Exception {
try {
Mysql mysql = new Mysql(selectsql);
mysql.setString(1, userid);
ResultSet rs = mysql.executeQuery();
boolean next = rs.next();
if (next) {
username = CONST.toChinese(rs.getString("username"));
email = CONST.toChinese(rs.getString("email"));
gender = rs.getInt("gender");
occupation = rs.getInt("occupation");
location = CONST.toChinese(rs.getString("location"));
city = CONST.toChinese(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;
mysql.close();
mysql = null;
return next;
}
catch (Exception ex) {
throw new Exception("Profile.update()" + ex.getMessage());
}
}
public boolean isValid() {
boolean valid = true;
if (userid == null || userid.equals(""))
valid = false;
if (username == null || username.equals(""))
valid = false;
return valid;
}
public boolean EmailExist() throws Exception {
boolean exist = false;
String sql = "select * from profile where email = ?";
try {
Mysql mysql = new Mysql(sql);
mysql.setString(1, email);
ResultSet rs = mysql.executeQuery();
if (rs.next()) {
exist = true;
}
rs.close();
rs = null;
mysql.close();
mysql = null;
}
catch (Exception ex) {
throw new Exception("Profile.update()" + ex.getMessage());
}
return exist;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -