有一个jsp页面用来展示数据库商品信息的,通过点击每一条记录后边的修改按钮跳转到修改页面,在修改页面先获取要修改的那个商品的信息,除了id不可改,其他内容是可改变的,点击修改页面的修改按钮,将这个修改表单提交到action类处理,在action类中调用update方法,update方法是在itemDao.java里边定义的。
以下是我的个文件代码:
1.原展示商品页面的代码:
2.修改页面代码
3.action类代码:
public class uploadttAction extends ActionSupport {
private File uploadtt;
private String uploadttContentType;
private String uploadttFileName;
private String result;
private String className;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className= className;
}
private int id; // 产品编号
private String name; // 产品名称
private String huohao;//产品货号
private int price; // 产品价格
private String pic; // 产品图片
private String descri; // 产品描述
private String form; //组件
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getHuohao() {
return huohao;
}
public void setHuohao(String huohao) {
this.huohao = huohao;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public String getForm() {
return form;
}
public void setForm(String form) {
this.form = form;
}
public String getPic() {
return pic;
}
public void setPic(String pic) {
this.pic = pic;
}
public String getDescri() {
return descri;
}
public void setDescri(String descri) {
this.descri = descri;
}
public File getUploadtt() {
return uploadtt;
}
public void setUploadtt(File upload) {
this.uploadtt = upload;
}
public String getUploadttContentType() {
return uploadttContentType;
}
public void setUploadttContentType(String uploadContentType) {
this.uploadttContentType = uploadContentType;
}
public String getUploadttFileName() {
return uploadttFileName;
}
public void setUploadttFileName(String uploadFileName) {
this.uploadttFileName = uploadFileName;
}
public String getResult() {
return result;
}
public void setResult(String result) {
this.result = result;
}
// 实现execute方法
public String execute() throws Exception {
HttpServletRequest reqeust = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
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.setClassName(className);
n.setName(name);
n.setHuohao(huohao);
n.setForm(form);
n.setDescri(descri);
n.setPrice(price);
n.setPic(pic);
try {
iDao.updateItem(n);
} catch (SQLException e) {
e.printStackTrace();
}
//}
return SUCCESS;
}
}
4.Dao类代码:
public void updateItem(item item) throws SQLException {
Connection conn = DBUtil.getConnection();
try {
String sql ="update item set className=?,name=?,huohao=?,form=?,descri=?,pic=?,price=? where id=?";
PreparedStatement ptmt = conn.prepareStatement(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();
} catch (Exception e) {
e.printStackTrace();
}
}