通过点击表格记录中的修改按钮,转到修改页面,在action类中获取修改后的数据,调用dao类的更新操作,运行后不报错,但是也没有更新数据,为什么呀

两颗大白菜 2017-05-27 10:13:45
原先获取数据库记录的jsp:


点击修改后转到的jsp:


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.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);
} catch (SQLException e) {
e.printStackTrace();
}
//}

return SUCCESS;

}

}


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();
}
}
...全文
357 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
阳光越来越暖 2017-05-31
  • 打赏
  • 举报
回复
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(); } } 不是从0开始的嘛? sql执行了之后如果没有报错也没有 效果 应该是没有 提交事务
两颗大白菜 2017-05-31
  • 打赏
  • 举报
回复
引用 5 楼 zc881124 的回复:
[quote=引用 楼主 qq_38881243 的回复:] 原先获取数据库记录的jsp: 点击修改后转到的jsp: 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.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);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		//}

		return SUCCESS;

	}

}
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();
		}
	}
下标是从1开始,首先你把//ptmt.setInt(8, item.getId()); 注释去掉,将执行的完整sql的语句打印出来, 如果你id传值有误,相当于没找到这条记录,就不会更新,也不会报错, 另将executeUpdate()的返回值打印出来。[/quote] 是在不懂,能否加个Q交流一下
110成成 2017-05-31
  • 打赏
  • 举报
回复
引用 楼主 qq_38881243 的回复:
原先获取数据库记录的jsp: 点击修改后转到的jsp: 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.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);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		//}

		return SUCCESS;

	}

}
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();
		}
	}
下标是从1开始,首先你把//ptmt.setInt(8, item.getId()); 注释去掉,将执行的完整sql的语句打印出来, 如果你id传值有误,相当于没找到这条记录,就不会更新,也不会报错, 另将executeUpdate()的返回值打印出来。
两颗大白菜 2017-05-31
  • 打赏
  • 举报
回复
引用 3 楼 u012318208 的回复:
提交事务 con.commit(); con.setAutoCommit(true);
不起作用,啊啊啊,到底是哪里错了
sunflyflyfly 2017-05-31
  • 打赏
  • 举报
回复
提交事务 con.commit(); con.setAutoCommit(true);
两颗大白菜 2017-05-31
  • 打赏
  • 举报
回复
引用 1 楼 qq_35261789 的回复:
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(); } } 不是从0开始的嘛? sql执行了之后如果没有报错也没有 效果 应该是没有 提交事务
1开始也没有报错,怎么提交事物

51,412

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧