N977_ 2019-05-09 04:01 采纳率: 100%
浏览 911
已采纳

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

小白,用的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>
  • 写回答

2条回答 默认 最新

  • 专家-郭老师 Java领域新星创作者 2019-05-09 09:58
    关注
      String notice = request.getParameter("notice");
            System.out.println(notice);//可以在console看到修改的数据
            NoticeBean nb = new NoticeBean();        这里没有数据,    你的notice也没有添加进实体类。
            NoticeDao dao = new NoticeDao();
            try {
                dao.updateNotice(nb);       //相当于传了空对象
                response.sendRedirect("admain.jsp");
            } catch (Exception e1) {
                e1.printStackTrace();
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办