📄 user.java
字号:
/*
* 创建日期 2005-3-6
*
* TODO 要更改此生成的文件的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
package store;
/**
* @author wwx
*
* TODO 要更改此生成的类型注释的模板,请转至
* 窗口 - 首选项 - Java - 代码样式 - 代码模板
*/
import store.ParamUtils;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.ResultSet;
import java.util.Date;
public class User {
private long id;
private String username;
private String password;
private String name;
private String sex;
private String address;
private String phone;
private String post;
private String email;
private String regtime;
public User()
{
}
public void SetId(long l)
{
id = l;
}
public void SetUsername(String s)
{
username = s;
}
public void SetPassword(String s)
{
password = s;
}
public void SetName(String s)
{
name = s;
}
public void SetSex(String s)
{
sex = s;
}
public void SetAddress(String s)
{
address = s;
}
public void SetPhone(String s)
{
phone = s;
}
public void SetPost(String s)
{
post = s;
}
public void SetEmail(String s)
{
email = s;
}
public void SetRegtime(String s)
{
regtime = s;
}
public String GetId()
{
return String.valueOf(id);
}
public String GetUsername()
{
return username;
}
public String GetPassword()
{
return password;
}
public String GetName()
{
return name;
}
public String GetSex()
{
return sex;
}
public String GetAddress()
{
return address;
}
public String GetPhone()
{
return phone;
}
public String GetPost()
{
return post;
}
public String GetEmail()
{
return email;
}
public String GetRegtime()
{
return regtime;
}
public static void LoginUser(HttpServletRequest httpservletrequest)
throws Exception
{
boolean flag = false;
String s = "";
String s1 = ParamUtils.getString(httpservletrequest, "username", "");
String s2 = ParamUtils.getString(httpservletrequest, "password", "");
if(s1.equals("") || s1 == null)
{
s = s + "You have not input your username!<br>";
flag = true;
}
if(s2.equals("") || s2 == null)
{
s = s + "You have not input your password!<br>";
flag = true;
}
if(!flag)
{
DBConnect dbconnect = new DBConnect();
//从库中查找该登陆用户
dbconnect.prepareStatement("SELECT * FROM My_users WHERE username = ? and password = ?");
dbconnect.setString(1, s1);
dbconnect.setString(2, s2);
ResultSet resultset = dbconnect.executeQuery();
if(resultset.next()) //如存在...
{
User user = new User();
//把从库中读出的记录赋给user
user.SetUsername(s1);
user.SetPassword(s2);
user.SetId(resultset.getLong("id"));
user.SetName(resultset.getString("name"));
user.SetSex(resultset.getString("sex"));
user.SetAddress(resultset.getString("address"));
user.SetPhone(resultset.getString("phone"));
user.SetPost(resultset.getString("post"));
user.SetEmail(resultset.getString("email"));
user.SetRegtime(resultset.getString("regtime"));
//把user保存在session中,用户退出时删除
HttpSession httpsession = httpservletrequest.getSession(true);
httpsession.setAttribute("wwx", user);
httpsession.setMaxInactiveInterval(3600);
resultset.close();
} else
{
s = s + "Your username or password is error!<br>";
flag = true;
}
dbconnect.close();
}
if(flag)
throw new Exception(s);
else
return;
}
public static void LogoutUser(HttpSession httpsession) throws Exception
{
httpsession.removeAttribute("wwx");
}
public boolean AddUser(HttpServletRequest httpservletrequest)
throws Exception
{
String s = null;
boolean flag = false;
String s1 = ParamUtils.getString(httpservletrequest, "username", "");
String s2 = ParamUtils.getString(httpservletrequest, "password", "");
String s3 = ParamUtils.getString(httpservletrequest, "name", "");
String s4 = ParamUtils.getString(httpservletrequest, "sex", "");
String s5 = ParamUtils.getString(httpservletrequest, "address", "");
String s6 = ParamUtils.getString(httpservletrequest, "phone", "");
String s7 = ParamUtils.getString(httpservletrequest, "post", "");
String s8 = ParamUtils.getString(httpservletrequest, "email", "");
if(s1.equals("") || s1 == null)
{
s = s + "You have not input your username!<br>";
flag = true;
}
if(s2.equals("") || s2 == null)
{
s = s + "You have not input password!<br>";
flag = true;
}
if(s3.equals("") || s3 == null)
{
s = s + "You have not input your true name!<br>";
flag = true;
}
if(s4.equals("") || s4 == null)
{
s = s + "You have not input sex!<br>";
flag = true;
}
if(s5.equals("") || s5 == null)
{
s = s + "You have not input your address!<br>";
flag = true;
}
if(s6.equals("") || s6 == null)
{
s = s + "You have not input your phon number!<br>";
flag = true;
}
if(s7.equals("") || s7 == null)
{
s = s + "You have not input your postalcode!<br>";
flag = true;
}
if(!flag)
{
//检查用户名是否已经存在
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("select * from my_users where username=?");
dbconnect.setString(1, s1);
ResultSet rs = dbconnect.executeQuery();
if(rs.next())
{
s = s + "The username '" + s1 + "' has exist!<br>";
flag = true;
}
rs.close();
dbconnect.close();
}
if(!flag)
{
//添加用户注册资料到库中
Date udate = new Date();
java.sql.Date date = new java.sql.Date(udate.getTime());
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("insert into my_users (username,password,name,sex,address,phone,post,email,regtime) values (?,?,?,?,?,?,?,?,?)");
dbconnect.setString(1, s1);
dbconnect.setString(2, s2);
dbconnect.setString(3, s3);
dbconnect.setString(4, s4);
dbconnect.setString(5, s5);
dbconnect.setString(6, s6);
dbconnect.setString(7, s7);
dbconnect.setString(8, s8);
dbconnect.setDate(9, date);
dbconnect.executeUpdate();
dbconnect.close();
}
if(flag)
throw new Exception(s);
else
return true;
}
public static void ChkLogin(HttpSession httpsession) throws Exception
{
boolean flag = false;
String s1 = "";
User user = (User)httpsession.getAttribute("wwx");
if(user==null)
{
s1 = s1 + "You have not loged in!<br>";
flag = true;
}
if(flag)
throw new Exception(s1);
else
return;
}
public static void ChkLogin(HttpSession httpsession, String pass) throws Exception
{
boolean flag = false;
String s1 = "";
User user = (User)httpsession.getAttribute("wwx");
if(user==null)
{
s1 = s1 + "You have not loged in!<br>";
flag = true;
}
else if(!user.GetPassword().equals(pass))
{
s1 = s1 + "Your password is wrong!<br>";
flag = true;
}
if(flag)
throw new Exception(s1);
else
return;
}
public boolean EditUser(HttpServletRequest httpservletrequest) throws Exception
{
String s = null;
boolean flag = false;
String s0 = ParamUtils.getString(httpservletrequest, "newpassword", "");
String s1 = ParamUtils.getString(httpservletrequest, "id", "");
String s2 = ParamUtils.getString(httpservletrequest, "username", "");
String s3 = ParamUtils.getString(httpservletrequest, "password", "");
String s4 = ParamUtils.getString(httpservletrequest, "name", "");
String s5 = ParamUtils.getString(httpservletrequest, "sex", "");
String s6 = ParamUtils.getString(httpservletrequest, "address", "");
String s7 = ParamUtils.getString(httpservletrequest, "phone", "");
String s8 = ParamUtils.getString(httpservletrequest, "post", "");
String s9 = ParamUtils.getString(httpservletrequest, "email", "");
if(s1.equals("") || s1 == null)
{
s = s + "You have not input id!<br>";
flag = true;
}
if(s2.equals("") || s2 == null)
{
s = s + "You have not password!<br>";
flag = true;
}
if(s4.equals("") || s4 == null)
{
s = s + "You have not input true name!<br>";
flag = true;
}
if(s5.equals("") || s5 == null)
{
s = s + "You have not sex!<br>";
flag = true;
}
if(s6.equals("") || s6 == null)
{
s = s + "You have not input address!<br>";
flag = true;
}
if(s7.equals("") || s7 == null)
{
s = s + "You have not input phon number!<br>";
flag = true;
}
if(s8.equals("") || s8 == null)
{
s = s + "You have not input postalcode!<br>";
flag = true;
}
if(!flag)
{
//更新用户注册资料
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("update my_users set username=?, password=?,name=?,sex=?,address=?,phone=?,post=?,email=? where id=?");
dbconnect.setString(1, s2);
if(s0.equals("") || s0 == null)
{
dbconnect.setString(2, s3);
}
else
{
dbconnect.setString(2, s0);
}
dbconnect.setString(3, s4);
dbconnect.setString(4, s5);
dbconnect.setString(5, s6);
dbconnect.setString(6, s7);
dbconnect.setString(7, s8);
dbconnect.setString(8, s9);
dbconnect.setLong(9, Long.parseLong(s1));
dbconnect.executeUpdate();
dbconnect.close();
}
if(flag)
throw new Exception(s);
else
return true;
}
public static void DelUser(String delid) throws Exception
{
String s = "";
boolean flag = false;
if (delid.equals("") || delid==null)
{
s = "You must input the id of the user which you want to delete!<br>";
flag = true;
}
if (!flag)
{
//先删除该用户的订单信息
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("delete from My_users where id = ?");
dbconnect.setLong(1, Long.parseLong(delid));
dbconnect.executeUpdate();
}
else
{
throw new Exception(s);
}
}
public void DelUser(HttpServletRequest httpservletrequest) throws Exception
{
String s = "";
boolean flag = false;
String s1 = ParamUtils.getString(httpservletrequest, "field", "");
String s2 = ParamUtils.getString(httpservletrequest, "keyword", "");
if(s1.equals("") || s1==null)
{
s1 = (String)httpservletrequest.getAttribute("field");
}
if(s2.equals("") || s2==null)
{
s2 = (String)httpservletrequest.getAttribute("keyword");
}
if(s1.equals("") || s1==null)
{
s = s + "You have not input the field!<br>";
flag = true;
}
if(s2.equals("") || s2==null)
{
s = s + "You have not input the keyword!<br>";
flag = true;
}
if(!flag)
{
DBConnect dbconnect = new DBConnect();
//先删除Indent中相应的项
httpservletrequest.setAttribute("operator", "=");
Vector vector = LoadUser(httpservletrequest);
for(int i=0; i<vector.size(); i++)
{
User user = (User) vector.elementAt(i);
Indent indent = new Indent();
httpservletrequest.setAttribute("field","username");
httpservletrequest.setAttribute("keyword",user.GetUsername());
indent.DelIndent(httpservletrequest);
}
dbconnect.prepareStatement("delete from My_users where " + s1 + "=?");
if (s1.equals("id"))
{
dbconnect.setLong(1, Long.parseLong(s2));
}
else
{
dbconnect.setString(1, s2);
}
dbconnect.executeUpdate();
dbconnect.close();
}
if(flag)
{
throw new Exception(s);
}
}
public Vector LoadUser() throws Exception
{
Vector vector = new Vector();
String s = "";
boolean flag = false;
if(!flag)
{
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("select * from My_users");
User user;
for(ResultSet resultset = dbconnect.executeQuery(); resultset.next(); vector.add(user))
{
user = new User();
user.SetId(resultset.getLong("id"));
user.SetUsername(resultset.getString("username"));
user.SetPassword(resultset.getString("password"));
user.SetName(resultset.getString("name"));
user.SetSex(resultset.getString("sex"));
user.SetAddress(resultset.getString("address"));
user.SetPhone(resultset.getString("phone"));
user.SetPost(resultset.getString("post"));
user.SetEmail(resultset.getString("email"));
user.SetRegtime(resultset.getString("regtime"));
}
dbconnect.close();
}
if(flag)
{
throw new Exception(s);
}
else
{
return vector;
}
}
public Vector LoadUser(HttpServletRequest httpservletrequest) throws Exception //读取指定用户信息
{
Vector vector = new Vector();
String s = "";
boolean flag = false;
String s1 = ParamUtils.getString(httpservletrequest, "field","");
String s2 = ParamUtils.getString(httpservletrequest, "keyword","");
String s3 = ParamUtils.getString(httpservletrequest, "operator","");
if(s1.equals("") || s1==null)
{
s1 = (String)httpservletrequest.getAttribute("field");
}
if(s2.equals("") || s2==null)
{
s2 = (String)httpservletrequest.getAttribute("keyword");
}
if(s3.equals("") || s3==null)
{
s3 = (String)httpservletrequest.getAttribute("operator");
}
if(s1.equals("") || s1==null)
{
s = s + "You have not input the field!<br>";
flag = true;
}
if(s2.equals("") || s2==null)
{
s = s + "You have not input the keyword!<br>";
flag = true;
}
if(s3.equals("") || s3==null)
{
s = s + "You have not input the operator!<br>";
flag = true;
}
if(!flag)
{
DBConnect dbconnect = new DBConnect();
dbconnect.prepareStatement("select * from My_users where " + s1 + s3 + "?");
if (s1.equals("id"))
{
dbconnect.setLong(1, Long.parseLong(s2));
}
else
{
dbconnect.setString(1, s2);
}
User user;
for(ResultSet resultset = dbconnect.executeQuery(); resultset.next(); vector.add(user))
{
user = new User();
user.SetId(resultset.getLong("id"));
user.SetUsername(resultset.getString("username"));
user.SetPassword(resultset.getString("password"));
user.SetName(resultset.getString("name"));
user.SetSex(resultset.getString("sex"));
user.SetAddress(resultset.getString("address"));
user.SetPhone(resultset.getString("phone"));
user.SetPost(resultset.getString("post"));
user.SetEmail(resultset.getString("email"));
user.SetRegtime(resultset.getString("regtime"));
}
dbconnect.close();
}
if(flag)
{
throw new Exception(s);
}
else
{
return vector;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -