小白,用的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>