diff --git a/src/com/landaiqing/dao/UserDao.java b/src/com/landaiqing/dao/UserDao.java index b355a3a..701441d 100644 --- a/src/com/landaiqing/dao/UserDao.java +++ b/src/com/landaiqing/dao/UserDao.java @@ -1,5 +1,6 @@ package com.landaiqing.dao; +import com.landaiqing.entity.AdminUserEntity; import com.landaiqing.entity.UserEntity; import com.landaiqing.utils.JdbcUtils; @@ -127,4 +128,37 @@ public class UserDao { JdbcUtils.closeConnection(null, preparedStatement, connection); } } + + /** + * 修改数据 + * */ + public int update(UserEntity userEntity) { + Connection connection = null; + PreparedStatement preparedStatement = null; + try { + connection = JdbcUtils.getConnection(); + JdbcUtils.beginTransaction(connection); + preparedStatement = connection.prepareStatement("UPDATE `webguestbook`.`user` SET `nickname` = ?, `qq` = ?, `email` = ?, `content` = ?, `datetime` = ?, `replyContent` = ?, `replyDateTime` = ? WHERE `userId` = ?;"); + + preparedStatement.setString(1, userEntity.getNickName()); + preparedStatement.setString(2, userEntity.getQQ()); + preparedStatement.setString(3, userEntity.getEmail()); + preparedStatement.setString(4, userEntity.getContent()); + preparedStatement.setDate(5, userEntity.getDateTime()); + preparedStatement.setString(6, userEntity.getReplyContent()); + preparedStatement.setDate(7, userEntity.getReplyDateTime()); + preparedStatement.setInt(8, userEntity.getUserId()); + Integer result = preparedStatement.executeUpdate(); + JdbcUtils.commitTransaction(connection); + return result; + + } catch (SQLException e) { + JdbcUtils.rollBackTransaction(connection); + throw new RuntimeException(e); + + } finally { + JdbcUtils.closeConnection(null, preparedStatement, connection); + } + + } } diff --git a/src/com/landaiqing/service/UserService.java b/src/com/landaiqing/service/UserService.java index 12e283a..a3623df 100644 --- a/src/com/landaiqing/service/UserService.java +++ b/src/com/landaiqing/service/UserService.java @@ -34,4 +34,10 @@ public class UserService { public int deleteUser(Integer id){ return userDao.deleteUser(id); } + /** + * 修改数据 + * */ + public int update(UserEntity userEntity) { + return userDao.update(userEntity); + } } diff --git a/src/com/landaiqing/servlet/system/UpdateServlet.java b/src/com/landaiqing/servlet/system/UpdateServlet.java new file mode 100644 index 0000000..d6bb282 --- /dev/null +++ b/src/com/landaiqing/servlet/system/UpdateServlet.java @@ -0,0 +1,99 @@ +package com.landaiqing.servlet.system; + +import com.landaiqing.entity.AdminUserEntity; +import com.landaiqing.entity.UserEntity; +import com.landaiqing.service.UserService; +import com.mysql.cj.util.StringUtils; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +import java.io.IOException; +import java.sql.Date; + +@WebServlet("/update") +public class UpdateServlet extends HttpServlet { + private UserService userService=new UserService(); + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + try { + String idStr = req.getParameter("id"); + if (StringUtils.isNullOrEmpty(idStr)){ + req.setAttribute("errorMsg","id的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + Integer id = Integer.parseInt(idStr); + + String nickName = req.getParameter("nickName"); + if (StringUtils.isNullOrEmpty(nickName)){ + req.setAttribute("errorMsg","nickName 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String qq = req.getParameter("qq"); + if (StringUtils.isNullOrEmpty(qq)){ + req.setAttribute("errorMsg","qq 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String email = req.getParameter("email"); + if (StringUtils.isNullOrEmpty(idStr)){ + req.setAttribute("errorMsg","email 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + String content = req.getParameter("content"); + if (StringUtils.isNullOrEmpty(idStr)){ + req.setAttribute("errorMsg","content 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + String DateTime = req.getParameter("dateTime"); + if (StringUtils.isNullOrEmpty(idStr)){ + req.setAttribute("errorMsg","DateTime 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String replyContent = req.getParameter("replyContent"); + if (StringUtils.isNullOrEmpty(idStr)){ + req.setAttribute("errorMsg","replyContent 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + String replyDateTime = req.getParameter("replyDateTime"); + if (StringUtils.isNullOrEmpty(idStr)){ + req.setAttribute("errorMsg","replyDateTime 的值不能为空!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + + + UserEntity userEntity=new UserEntity(); + userEntity.setUserId(id); + userEntity.setNickName(nickName); + userEntity.setQQ(qq); + userEntity.setEmail(email); + userEntity.setContent(content); + userEntity.setDateTime(Date.valueOf(DateTime)); + userEntity.setReplyContent(replyContent); + userEntity.setReplyDateTime(Date.valueOf(replyDateTime)); + + int result = userService.update(userEntity); + if (result<=0){ + req.setAttribute("errorMsg","修改失败!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + return; + } + resp.sendRedirect("./System/messageManage.jsp"); + } catch (Exception e) { + req.setAttribute("errorMsg","系统异常!!!"); + req.getRequestDispatcher("error.jsp").forward(req,resp); + throw new RuntimeException(e); + } + } +} diff --git a/web/System/adminManage.jsp b/web/System/adminManage.jsp index 4ce2a2e..346d345 100644 --- a/web/System/adminManage.jsp +++ b/web/System/adminManage.jsp @@ -296,6 +296,7 @@ layer.msg('修改成功!'), { offset: '6px' } + window.location.reload(); }) } @@ -313,6 +314,7 @@ layer.msg('删除成功!'), { offset: '6px' } + window.location.reload(); }) } function addAdmin() @@ -335,6 +337,7 @@ layer.msg('添加成功!'), { offset: '6px' } + window.location.reload(); }) } diff --git a/web/System/messageManage.jsp b/web/System/messageManage.jsp index 76faf9f..b6e62a8 100644 --- a/web/System/messageManage.jsp +++ b/web/System/messageManage.jsp @@ -19,10 +19,11 @@ + -