我有一个数据库表,其中已经存储了一些数据。
现在我希望在我的页面中有一个Vaadin 可编辑的表,其中包含从数据库中获取的数据,当更新此Vaadin表时(添加一行或编辑现有的),数据库应该得到更新。
我非常接近,但我意识到我不能只使用INSERT查询,因为违反了唯一的ID约束。
为了达到这个目的,我该怎么办?我真的需要你的帮助!
BeanItemContainer :
public BeanItemContainer createContainer() {
BeanItemContainer<Sct> beans = new BeanItemContainer<Sct>(Sct.class);
beans.addNestedContainerProperty("secti.sid");
beans.addNestedContainerProperty("secti.isa");
beans.addNestedContainerProperty("secti.order");
for (int i = 0; i < list.size(); i++) {
PS_SECTION section = list.get(i);
Long ps = section.getPS_SECTION();
String na = section.getNAME();
Long is = section.getISACTIVE();
Long or = section.getVORDER();
Object itemId = beans.addBean(new Sct(na, new BeanSect(ps, is, or)));
}
return beans;
}
点击后点击保存(更新)按钮:
ClickListener saveListener = new ClickListener() {
private Long s4 = 0L;
@Override
public void buttonClick(ClickEvent event) {
Collection<?> itemIds = table.getItemIds();
Item item = null;
boolean isSaved = false;
PS_SECTION ps = null;
List<PS_SECTION> newlist = new ArrayList<PS_SECTION>();
int i = 0;
for(Object itemId : itemIds){
ps = new PS_SECTION();
item = table.getItem(itemId);
Long s1 = (Long) item.getItemProperty("ID").getValue();
String s2 = item.getItemProperty("SECTION").getValue().toString();
Long s3 = (Long) item.getItemProperty("ORDER").getValue();
Long s5 = 0L;
ps.setPS_SECTION(s1);
ps.setNAME(s2);
ps.setVORDER(s3);
ps.setISACTIVE(s4);
ps.setISGLOBAL(s5);
newlist.add(ps);
}
try {
// Here I call the query.
isSaved = dao.insertPsSections(newlist);
if (isSaved){
Notification.show("Success ");
unsavedChanges = false;
}
else if (!isSaved){
Notification.show("Problem");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
最后,查询:
public boolean insertPsSections(List<PS_SECTION> pssec) throws SQLException {
Boolean sv = false;
try {
this.setKeepConnOpen(true);
String insertsections = "INSERT INTO PS_SECTION"
+ "(PS_SECTION,NAME,ISACTIVE,ISGLOBAL,VORDER)"
+ "VALUES (?,?,?,?,?)";
for (PS_SECTION j : pssec){
Long section = j.getPS_SECTION();
String name = j.getNAME();
Long isa = j.getISACTIVE();
Long isg = j.getISGLOBAL();
Long order = j.getVORDER();
this.simpleUpdate(insertsections, false, section, name, isa, isg, order);
}
} finally {
this.doCommit();
this.closeConn(true);
sv=true;
}
return sv;
}