该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
急急急急,在线等,我是通过id获取数据库原记录,在修改页面,id设置为只读,其他可改,将修改后的表单提交action类处理,在action类中调用update方法,但是没有更新成功,我用的是sqlserver 2005,求解答
以下是action类代码:
public String execute() throws Exception {
HttpServletRequest reqeust = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
//PrintWriter out = response.getWriter();
String path = ServletActionContext.getServletContext().getRealPath(
"/img");
File file = new File(path);
if (!file.exists()) {
file.mkdir();
}
FileUtils.copyFile(uploadtt, new File(file, uploadttFileName));
result = "上传成功";
item n = new item();
itemDao iDao = new itemDao();
//int id = Integer.parseInt(reqeust.getParameter("id"));
String className = reqeust.getParameter("className");
String name = (String) reqeust.getParameter("name");
String huohao = (String) reqeust.getParameter("aNumber");
String form = (String) reqeust.getParameter("component");
String descri = (String) reqeust.getParameter("descri");
int price = Integer.parseInt(reqeust.getParameter("price"));
String pic = "img/" + uploadttFileName; // 获取表单传过来的数据
//n.setId(id);
n.setClassName(className);
n.setName(name);
n.setHuohao(huohao);
n.setForm(form);
n.setDescri(descri);
n.setPrice(price);
n.setPic(pic);
try {
iDao.updateItem(n);
System.out.println("更新的数据为"+className+name+huohao+form+descri+price+pic);
} catch (SQLException e) {
e.printStackTrace();
}
return SUCCESS;
}
}
以下是update方法:
public void updateItem(item item) throws SQLException {
Connection conn = DBUtil.getConnection();
conn.setAutoCommit(false);
try {
String sql ="update item set className=?,name=?,huohao=?,form=?,descri=?,pic=?,price=? where id=?";
PreparedStatement ptmt = conn.prepareStatement(sql);
System.out.println(sql);
ptmt.setString(1, item.getClassName());
ptmt.setString(2, item.getName());
ptmt.setString(3, item.getHuohao());
ptmt.setString(4, item.getForm());
ptmt.setString(5, item.getDescri());
ptmt.setString(6, item.getPic());
ptmt.setInt(7, item.getPrice());
ptmt.setInt(8, item.getId());
ptmt.executeUpdate();
conn.commit();
System.out.println("Success!");
conn.setAutoCommit(true);
} catch (Exception e) {
e.printStackTrace();
}
}
以下是控制台的输出信息:
update item set className=?,name=?,huohao=?,form=?,descri=?,pic=?,price=? where id=?
Success!
更新的数据为茶席组合21201705441一壶四倍三稍a10001img/goods.JPG