gpt4 book ai didi

java - DataTable - InCell 编辑---如何用编辑后的值更新数据库

转载 作者:行者123 更新时间:2023-12-02 00:12:18 25 4
gpt4 key购买 nike

 I have a managed bean called registerBean.

private List<registerBean> std;

public void onEdit(RowEditEvent event) {
registerBean ul = (registerBean) event.getObject();
Connection con = null;
PreparedStatement stat = null;
ResultSet rs = null;

try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db", "root", "");
stat = con.prepareStatement("update regtbl set fname=? where rno=?");
stat.setString(1, ul.fname);
stat.setInt(3, ul.rno);
stat.executeUpdate();
con.commit();
stat.close();
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}

This is my Facelets page

std.xhtml

<!-- language: xhtml -->

<p:dataTable value="#{registerBean.list}" var="rb" rows="10" paginator="true"
paginatorTemplate=" {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,15,20" rowKey="#{rb.fname}" selectionMode="single"
selection="#{registerBean.selectedStudent}"
scrollable="true" resizableColumns="true" editable="true" >
<p:ajax event="rowEdit" listener="#{registerBean.onEdit}"
update=":form:messages" />
<p:ajax event="rowEditCancel" listener="#{registerBean.onCancel}"
update=":form:messages" />
<p:column headerText="Roll No" filterBy="#{rb.rno}" sortBy="#{rb.rno}" width="124">
<h:outputText value="#{rb.rno}" />
</p:column>
<p:column headerText="First Name" filterBy="#{rb.fname}" sortBy="#{rb.fname}" width="124">
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{rb.fname}" />
</f:facet>
<f:facet name="input">
<h:inputText value="#{rb.fname}" />
</f:facet>
</p:cellEditor>
</p:column>
<!-- more columns... -->
</p:dataTable>

Am I going wrong some where here ..... this method is called to save the edited rows of datatable.... but database records are not getting updated. When I edit a particular row in datatable and click on right mark to save the changes onEdit method is being called which is inside bean. The issue is database value are remaining the same not getting updated list is of type registerBean. In registerBean I am performing other operations like Inserting (working fine ), getting values from database that are displayed in datatable ... facing problem in row editing.

Thanks in Advance

Note: Check [this link][1]. My table is similar to this but i am using database.

[1]: http://www.primefaces.org/showcase/ui/datatableEditing.jsf

最佳答案

除了missing finally block 、不必要的重复 Class#forName() 调用和 completely unnecessary newInstance() 调用它,tight-coupling Controller 类中的数据访问代码,不使用快速连接池,所有这些都与具体问题无关,到目前为止发布的代码看起来还不错。

只是,您将 rno 值设置为第三个参数,而准备好的 SQL 字符串中只有 2 个参数。

    stat = con.prepareStatement("update regtbl set fname=? where rno=?");
stat.setString(1, ul.fname);
stat.setInt(3, ul.rno);

我不确定这是否是粗心的过度简化,或者您是否确实阅读了服务器日志以获取 e.printStackTrace() 的任何指示,但这是不对的。确保 ul.rno 设置在参数索引 2 上。我还想修复那个愚蠢的 e.printStackTrace(),它会导致代码继续 运行您显然不希望发生的情况。

} catch (Exception e) {
throw new FacesException(e);
}

如果 SQL 确实是正确的,并且您确实没有遇到异常,那么显然根本没有与 WHERE 子句匹配的行。 PreparedStatement#executeUpdate()返回一个代表受影响行的int。我建议获取它并验证是否确实有更新:

int affectedRows = stat.executeUpdate();

if (affectedRows == 0) {
// No rows affected!
}

还要检查您设置的 ul.rno 值。

<小时/>

更新 最后您花了一点力气来阅读服务器日志以获取异常的任何证据。您应该明白,了解异常非常重要,因为它们基本上是您具体问题的完整答案。您检索到的内容基本上在下面的代码中说明了这一点

public String getBdate() {
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
bdate = sdf.format(dob);
return bdate;
}

变量dobnull。您应该进行空检查:

public String getBdate() { 
if (dob != null) {
bdate = new SimpleDateFormat(DATE_FORMAT).format(dob);
}

return bdate;
}

关于java - DataTable - InCell 编辑---如何用编辑后的值更新数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12519999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com