我想删除连接到Mysql数据库的dataTable行。但当我点击dataTable内部的删除按钮时,没有任何事情发生(没有错误,行没有被删除)我在互联网上尝试了几种方法但没有任何反应,同样的事情没有错误也没有结果。我想提一下我是JSF的新手。
我在下面的代码中引用了以下代码http://www.devmanuals.com/tutorials/java/jsf/database/deleteData.html
这是我的代码,
dataEditing.xhtml
<form>
<div id="eTable">
<p:dataTable value="#{feeding.updateData()}" var="avv">
<p:column headerText="No.">
<h:outputText value="#{avv.ID}" />
</p:column>
<p:column headerText="Utterance">
<h:outputText value="#{avv.editUtterance}" />
</p:column>
<p:column headerText="Device response">
<h:outputText value="#{avv.editDeviceResponse}" />
</p:column>
<p:column headerText="Comments">
<h:outputText value="#{avv.editComments}" />
</p:column>
<p:column>
<center> <p:commandButton value="Delete" action="#{feeding.delete(avv.ID)}"/> </center>
</p:column>
</p:dataTable>
</div>
这是我的托管bean代码,
feeding.java
List<feeding> editboard;
public List<feeding> getEditBoard() {
return editboard;
}
public List<feeding> updateData() {
editboard = new ArrayList<feeding>();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/combi", "root", "#doubleinfinity7");
ResultSet rs = null;
conn.setAutoCommit(false);
String str = "select * from device_assessment";
PreparedStatement pst = conn.prepareStatement(str);
rs = pst.executeQuery(str);
while (rs.next()) {
feeding usrr = new feeding();
usrr.setEditUtterance(rs.getString("utterance"));
usrr.setEditDeviceResponse(rs.getString("response"));
usrr.setEditComments(rs.getString("comments"));
usrr.setID(rs.getLong("ID"));
editboard.add(usrr);
Map<Long, Boolean> checked = new HashMap<Long, Boolean>();
List<feeding> checkedItems = new ArrayList<feeding>();
for (feeding item : editboard) {
if (checked.get(item.getID()) != null) {
checkedItems.add(item);
usrr.delete(usrr.getID());
}
}
}
} catch (ClassNotFoundException | SQLException e) {
throw new FacesException(e);
}
return editboard;
}
public void delete(long ID) {
if (ID != 0) {
System.out.println(" current ID : " + ID);
FacesContext context = FacesContext.getCurrentInstance();
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/combi", "root", "#doubleinfinity7");
conn.setAutoCommit(false);
String sql = "delete from device_assessment where ID=" + ID;
PreparedStatement pst = conn.prepareStatement(sql);
int i = pst.executeUpdate();
if (i > 0) {
System.out.println("Row deleted successfully");
}
conn.commit();
context.addMessage(null, new FacesMessage("Successfully", "Deleted"));
} catch (ClassNotFoundException | SQLException e) {
throw new FacesException(e);
}
}
}
提前致谢!!