我正在尝试在调用ajax onCellEdit事件方法时将数据表单元格编辑的值更新为数据库。在此方法中,我使用单元格编辑列的标题名称和更新查询中获得的新值,它工作正常。但在我的实际数据库的字段名称与标题名称不同,我不能更改数据库字段名称,也不能将数据库字段名称包含到数据表头。请帮助我如何更新到数据库。
我正在使用JDBC进行数据库操作。我的代码与下面完全相同。
我的JSF页面
<h:form id="form">
<p:growl id="messages" showDetail="true" />
<p:dataTable id="multiCars" var="car"
value="#{tableBean.mediumCarsModel}" paginator="true" rows="10"
selection="#{tableBean.selectedCars}" editable="true" editMode="cell">
<f:facet name="header">
Checkbox Based Selection
</f:facet>
<p:ajax event="cellEdit" listener="#{tableBean.onCellEdit}"
update=":form:messages" />
<p:column selectionMode="multiple" style="width:2%" />
<p:column headerText="Model" style="width:25%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.model}" />
</f:facet>
<f:facet name="input">
<p:inputText id="modelInput" value="#{car.model}"
style="width:96%" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Year" style="width:25%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.year}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{car.year}" style="width:96%" label="Year" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Manufacturer" style="width:25%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.manufacturer}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{car.manufacturer}" style="width:96%"
label="Year" />
</f:facet>
</p:cellEditor>
</p:column>
<p:column headerText="Color" style="width:25%">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{car.color}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{car.color}" style="width:96%" label="Year" />
</f:facet>
</p:cellEditor>
</p:column>
<f:facet name="footer">
<p:commandButton id="multiViewButton" value="View"
icon="ui-icon-search" actionListener="#{tableBean.getSelection}" />
</f:facet>
</p:dataTable>
我的托管bean onCellEdit方法
public void onCellEdit(CellEditEvent event) {
System.out.println("inside cell edit");
UIColumn col= event.getColumn();
DataTable o=(DataTable) event.getSource();
Object oldValue = event.getOldValue(); //here i get old value
Object newValue = event.getNewValue(); //new value
Car info=(Car)o.getRowData();
String newModel=info.getModel();//primary key
System.out.println("row index in event method "+event.getRowIndex());
System.out.println("old value"+oldValue);
System.out.println("new value"+newValue);
System.out.println("column2 :"+col.getHeaderText());//header text value
try{
updateStmt=connection.createStatement();
String query = "update test set " + col.getHeaderText() +"= '"+ newValue +"' where Model = '"+newModel+"'";
int i=updateStmt.executeUpdate(query);
if(i!=0)
System.out.println("updated!!!");
else
System.out.println("not updated!!!");
}
catch (Exception e) {
e.printStackTrace();
}
if(newValue != null && !newValue.equals(oldValue)) {
FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}