📄 userinfo.java~157~
字号:
package gxaccp.t07.guoneng_wei.petclinicmanagesystem.actionform;
import java.io.*;
import java.sql.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;
import gxaccp.t07.guoneng_wei.jdbc.*;
public class UserInfo
extends ActionForm implements Serializable {
public static final int REGISTER_SUCCESS = 0;
public static final int REGISTER_USERNAME_EXISTS = 1;
public static final int REGISTER_FAILURE = -1;
private String userID = "";
private String userName = "";
private String userPassword = "";
private boolean admin = false;
private String isAdmin = "0";
private String newPasswordOne = "";
private String newPasswordTowe = "";
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public void setUserPassword(String userPassword) {
if (userPassword != null) {
this.userPassword = userPassword;
}
}
public void setUserName(String userName) {
if (userName != null) {
this.userName = userName;
}
}
public void setAdmin(boolean admin) {
this.admin = admin;
}
public void setIsAdmin(String isAdmin) {
if (isAdmin != null) {
if ("0".equals(isAdmin) || "1".equals(isAdmin)) {
this.setAdmin("1".equals(isAdmin) ? true : false);
this.isAdmin = isAdmin;
}
}
}
public void setNewPasswordOne(String newPasswordOne) {
this.newPasswordOne = newPasswordOne;
}
public void setNewPasswordTowe(String newPasswordTowe) {
this.newPasswordTowe = newPasswordTowe;
}
public String getUserName() {
return userName;
}
public String getUserPassword() {
return userPassword;
}
public boolean isAdmin() {
return admin;
}
public String getIsAdmin() {
return isAdmin;
}
public String getNewPasswordOne() {
return newPasswordOne;
}
public String getNewPasswordTowe() {
return newPasswordTowe;
}
public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/** @todo: finish this method, this is just the skeleton.*/
String uName = httpServletRequest.getParameter("userName");
String pswd = httpServletRequest.getParameter("userPassword");
boolean flag = false;
ActionErrors errors = new ActionErrors();
if (uName == null || "".equals(uName)) {
errors.add("userNameState", new ActionError("用户名要填写"));
flag = true;
}
if (pswd == null || "".equals(pswd)) {
errors.add("userPasswordState", new ActionError("密码要填写"));
flag = true;
}
if (flag) {
return errors; //验证不合格者,返回封装了错误信息的ActionErrors对象
}
return null; ////验证合格者,返回null
}
public void reset(ActionMapping actionMapping,
HttpServletRequest servletRequest) {
userID = "";
userName = "";
userPassword = "";
admin = false;
isAdmin = "0";
newPasswordOne = "";
newPasswordTowe = "";
}
public boolean login() {
Connection con = DataBaseAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
try {
pst = con.prepareStatement(
"SELECT * FROM UserInfo WHERE UserName=? AND UserPassword=?");
pst.setString(1, this.getUserName());
pst.setString(2, this.getUserPassword());
rs = pst.executeQuery();
if (rs.next()) {
//ID UserName UserPassword IsAdmin
this.setUserID(rs.getString("ID"));
// this.setAdmin(rs.getString("IsAdmin").equals("1") ? true : false);
this.setIsAdmin(rs.getString("IsAdmin"));
return true;
}
}
catch (SQLException ex) {
}
finally {
DataBaseAccess.closeResultSet(rs);
DataBaseAccess.closeStatement(pst);
DataBaseAccess.closeConnection(con);
}
return false;
}
public int addNewUser() {
Connection con = DataBaseAccess.getConnection();
CallableStatement cst = null;
// proc_AddNewUser @OutState OUTPUT,'username','123456',0
try {
cst = con.prepareCall(
"{call proc_AddNewUser (?,?,?,?)}");
cst.registerOutParameter(1, java.sql.Types.INTEGER);
cst.setString(2, this.getUserName());
cst.setString(3, this.getUserPassword());
cst.setInt(4, Integer.parseInt(this.getIsAdmin()));
cst.execute();
return cst.getInt(1);
}
catch (SQLException ex) {
}
finally {
DataBaseAccess.closeStatement(cst);
DataBaseAccess.closeConnection(con);
}
return this.REGISTER_FAILURE;
}
public boolean deleteUser() {
Connection con = DataBaseAccess.getConnection();
PreparedStatement pst = null;
// proc_AddNewUser @OutState OUTPUT,'username','123456',0
try {
pst = con.prepareStatement("DELETE FROM UserInfo WHERE ID=?");
pst.setString(1, this.getUserID());
int state = pst.executeUpdate();
if (state > 0) {
return true;
}
}
catch (Exception ex) {
}
finally {
DataBaseAccess.closeStatement(pst);
DataBaseAccess.closeConnection(con);
}
return false;
}
public boolean updateUserPassword() {
Connection con = DataBaseAccess.getConnection();
PreparedStatement pst = null;
boolean insert = false;
try {
pst = con.prepareStatement(
"UPDATE UserInfo SET UserPassword=? WHERE UserName=? ");
pst.setString(1, this.getNewPasswordOne());
pst.setString(2, this.getUserName());
if (pst.executeUpdate() > 0) {
insert = true;
}
}
catch (Exception ex) {
System.out.println("!!!!!!!!!异常出现在获取宠物类型时的updateUserPassword方法里!!!!!!!!!");
}
finally {
DataBaseAccess.closeStatement(pst);
DataBaseAccess.closeConnection(con);
}
return insert;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -