修改数据时,页面正常跳转,但是实际数据并没有修改

小白,用的MyEclipse,jdbc链接的数据库,增删改查除了修改都可以实现。修改时,未提示代码错误,而且页面也可以正常跳转,但是数据没有修改成功,依然是之前的数据。

具体代码如下:

dao:

public void updateNotice(NoticeBean nb)  {
        Connection conn = DBUtil.getConnectDb();
        String sql = "update gonggao set notice = ? where id =?";
        PreparedStatement stm = null;
        try {
            stm = conn.prepareStatement(sql);
            stm.setString(1, nb.getNotice());
            stm.setInt(2, nb.getId());
            stm.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

servlet:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        int id = Integer.parseInt(request .getParameter("id"));
        String notice = request.getParameter("notice");
        System.out.println(notice);//可以在console看到修改的数据
        NoticeBean nb = new NoticeBean();
        NoticeDao dao = new NoticeDao();
        try {
            dao.updateNotice(nb);
            response.sendRedirect("admain.jsp");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

jsp:

<form action="UpdateNo" method="post">
<table style="position: relative; left: 50%" class="table">
<% NoticeBean nb = (NoticeBean)request.getAttribute("nb"); %>
<tbody>
  <tr class="row1">
   <td valign="middle" align="center">
   <input type="hidden" name="id" value="<%=nb.getId() %>"/>
   </td>
   <td><input type="text" class="inputgri" name="notice" style="width: 800px;height: 80px;" value="<%=nb.getNotice() %>"/>
   </td>                                
  </tr>
</tbody>
</table>
<button style="width: 30%; height: 35px; position: relative; top: -20px;right:20px"type="submit" value="提交" />
                    提交
                </form>


犯罪嫌疑人X
浏览 718回答 2
2回答

慕侠2389804

&nbsp;&nbsp;String&nbsp;notice&nbsp;=&nbsp;request.getParameter("notice"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(notice);//可以在console看到修改的数据 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NoticeBean&nbsp;nb&nbsp;=&nbsp;new&nbsp;NoticeBean();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;这里没有数据,&nbsp;&nbsp;&nbsp;&nbsp;你的notice也没有添加进实体类。 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NoticeDao&nbsp;dao&nbsp;=&nbsp;new&nbsp;NoticeDao(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dao.updateNotice(nb);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//相当于传了空对象 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;response.sendRedirect("admain.jsp"); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(Exception&nbsp;e1)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e1.printStackTrace(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

Helenr

1、在dao层打断点2、sql执行了没有3、事务是否提交了
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java