24-修改密码功能
概述
本文接着写smbms的修改密码的功能
功能不算难,核心就是数据库的update语句
实践
UserDao.java
package com.kuang.dao.user;
import com.kuang.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* 功能描述
*
* @since 2022-09-05
*/
public interface UserDao {
public User getLoginUser(Connection conn, PreparedStatement preparedStatement, ResultSet resultSet, String sql,
Object[] params);
// 修改当前用户密码
public int updataUserPwd(Connection conn, PreparedStatement preparedStatement, int id, String pwd);
}
UserDaoImpl.java
package com.kuang.dao.user;
import com.kuang.dao.BaseDao;
import com.kuang.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* 功能描述
*
* @since 2022-09-05
*/
public class UserDaoImpl implements UserDao {
@Override
public User getLoginUser(Connection conn, PreparedStatement preparedStatement, ResultSet resultSet, String sql,
Object[] params) {
User user = new User();
try {
resultSet = BaseDao.execute(conn, sql, params, preparedStatement, resultSet);
if (resultSet.next()) {
user.setAddress(resultSet.getString("address"));
user.setBirthday(resultSet.getDate("birthday"));
user.setUserCode(resultSet.getString("userCode"));
user.setUserName(resultSet.getString("userName"));
user.setUserPassword(resultSet.getString("userPassword"));
user.setUserRole(resultSet.getString("userRole"));
user.setCreatedBy(resultSet.getInt("createdBy"));
user.setCreationDate(resultSet.getDate("creationDate"));
user.setModifyBy(resultSet.getInt("modifyBy"));
user.setModifyDate(resultSet.getDate("modifyDate"));
user.setGender(resultSet.getInt("gender"));
user.setId(resultSet.getInt("id"));
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return user;
}
@Override
public int updataUserPwd(Connection conn, PreparedStatement preparedStatement, int id, String pwd) {
String sql = "update smbms_user set userPassword=? where id=?";
Object[] params = {pwd, id};
int result = 0;
try {
result = BaseDao.execute(conn, sql, params, preparedStatement);
} catch (SQLException throwables) {
throwables.printStackTrace();
}
return result;
}
}
UserService.java
package com.kuang.service;
import com.kuang.pojo.User;
/**
* 功能描述
*
* @since 2022-09-05
*/
public interface UserService {
public User getLoginUser(String UserCode);
// 更新用户名密码
public boolean updateUserPwd(int id, String pwd);
}
UserServiceImpl.java
package com.kuang.service;
import com.kuang.dao.BaseDao;
import com.kuang.dao.user.UserDao;
import com.kuang.dao.user.UserDaoImpl;
import com.kuang.pojo.User;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
/**
* 功能描述
*
* @since 2022-09-05
*/
public class UserServiceImpl implements UserService {
private Connection connection = BaseDao.getConnection();
private UserDao userDao;
public UserServiceImpl() {
userDao = new UserDaoImpl();
}
@Override
public User getLoginUser(String UserCode) {
String sql = "select * from smbms_user where userCode=?";
Object[] params = {UserCode};
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
User loginUser = userDao.getLoginUser(connection, preparedStatement, resultSet, sql, params);
BaseDao.closeResource(null, preparedStatement, resultSet);
return loginUser;
}
@Override
public boolean updateUserPwd(int id, String pwd) {
PreparedStatement preparedStatement = null;
int i = userDao.updataUserPwd(connection, preparedStatement, id, pwd);
Boolean flag = false;
if (i > 0) {
flag = true;
}
BaseDao.closeResource(null, preparedStatement, null);
return flag;
}
}
UserServlet.java
package com.kuang.servlet;
import com.kuang.pojo.User;
import com.kuang.service.UserService;
import com.kuang.service.UserServiceImpl;
import com.kuang.utils.Constants;
import com.mysql.jdbc.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* 功能描述
*
* @since 2022-09-07
*/
public class UserServlet extends HttpServlet {
private static final long serialVersionUID = 4477827924112311146L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
updatePwd(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
public void updatePwd(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
Object o = req.getSession().getAttribute(Constants.USER_SESSION);
String newPwd = req.getParameter("newpassword");
if (o != null && !StringUtils.isNullOrEmpty(newPwd)) {
User user = (User) o;
UserService userService = new UserServiceImpl();
boolean flag = userService.updateUserPwd(user.getId(), newPwd);
if (flag) {
req.setAttribute(Constants.MESSAGE, "修改成功,请重新登录");
// 需移除当前Session
req.getSession().removeAttribute(Constants.USER_SESSION);
} else {
req.setAttribute(Constants.MESSAGE, "修改失败");
}
} else {
req.setAttribute(Constants.MESSAGE, "新密码有问题");
}
req.getRequestDispatcher("/jsp/pwdmodify.jsp").forward(req, resp);
}
}
web.xml
<servlet>
<servlet-name>user</servlet-name>
<servlet-class>com.kuang.servlet.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>user</servlet-name>
<url-pattern>/jsp/user.do</url-pattern>
</servlet-mapping>
遗留
UserServlet.java这个文件中最后使用转发的方式转发到当前的密码修改页面,将返回的修改成功或失败的消息以cookie的方式塞回给req,前端页面接收这个而消息并显示,没有找到是在哪里实现的。
标签:24,java,修改,kuang,resultSet,密码,user,import,com From: https://www.cnblogs.com/Oh-mydream/p/16668272.html