老师,为什么无法修改留言信息,方法有错吗,求解,程序没有报错
/**
* 修改留言信息
* @param message
* @return
*/
public boolean updateMessages(Message message) {
Connection conn = ConnectionUtil.getConnection();
String sql = "UPDATE message SET user_id = ?, username = ?, title = ?, content = ?, create_time = ? WHERE id = ?";
PreparedStatement stmt = null;
try {
stmt = conn.prepareStatement(sql);
stmt.setLong(1,message.getUserId());
stmt.setString(2,message.getUsername());
stmt.setString(3, message.getTitle());
stmt.setString(4, message.getContent());
stmt.setTimestamp(5, new Timestamp(message.getCreateTime().getTime()));
stmt.setLong(6,message.getId());
stmt.execute();
} catch (SQLException e) {
System.out.println("查询用户信息失败。");
e.printStackTrace();
return false;
} finally {
ConnectionUtil.release(null, stmt, conn);
}
return true;
}
public class MyMessageServlet extends HttpServlet { private MessageService messageService; @Override public void init() throws ServletException { super.init(); messageService = new MessageService(); } @Override public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pathName = request.getServletPath(); Message message = (Message) request.getSession().getAttribute("messages"); if (Objects.equals("/editMessagePrompt.do", pathName)) { Long id = Long.valueOf(request.getParameter("id")); request.getSession().setAttribute("id",id); Message messages = messageService.getMessagesById(id); request.getSession().setAttribute("messages", messages); request.getRequestDispatcher("/WEB-INF/views/biz/updat_message.jsp").forward(request, response); } else if (Objects.equals("/editMessage.do", pathName)) { long id = (long) request.getSession().getAttribute("id"); long u_id = (long) request.getSession().getAttribute("u_id"); String username = message.getUsername(); Date createTime = message.getCreateTime(); String inputTitle = request.getParameter("title"); String inputContent = request.getParameter("content"); Message messsage = new Message(id,u_id,username,inputTitle,inputContent,createTime); boolean result = messageService.updateMessages(messsage); if(result){ String pageStr = (String) request.getSession().getAttribute("pageStr");//当前页码 Long sid = Long.valueOf(u_id);//从Session获得当前用户ID System.out.print(pageStr); int page = 1;//页码默认值为1 if (null != pageStr && (!"".equals(pageStr))) { try { page = Integer.parseInt(pageStr); } catch (NumberFormatException e) { e.printStackTrace(); } } List<Message> messages = messageService.mygetMessages(page, 5 ,sid);//分页查询当前用户留言 int count = messageService.mycountMessages(sid);//查询当前用户留言数量 int last = count % 5 == 0 ? (count / 5) : ((count / 5) + 1); request.setAttribute("last", last); request.setAttribute("messages", messages); request.setAttribute("page", page); request.getRequestDispatcher("/WEB-INF/views/biz/my_message_list.jsp").forward(request, response); }else { request.getRequestDispatcher("/WEB-INF/views/error/404.jsp").forward(request, response); } } else { request.getRequestDispatcher("/WEB-INF/views/error/404.jsp").forward(request, response); } } }
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星