简体   繁体   English

DataTable-InCell编辑-如何使用已编辑的值更新数据库

[英]DataTable - InCell Editing— how to update database with edited values

 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. 当我在数据表中编辑特定行并单击右标记以保存更改时,将调用位于bean内部的onEdit方法。 The issue is database value are remaining the same not getting updated list is of type registerBean. 问题是数据库值保持不变,未更新列表的类型为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. 在registerBean中,我正在执行其他操作,例如插入(正常工作),从数据库中获取显示在datatable中的值……在行编辑中面临问题。

 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 

Apart from the missing finally block , the unnecessarily repeated Class#forName() call and the completely unnecessary newInstance() call on it, tight-coupling data access code in a controller class, not using a fast connection pool, all which are unrelated to the concrete problem, the code posted so far looks okay. 除了缺少的finally块之外 ,不必要的重复Class#forName()调用和对其完全不必要的 newInstance()调用,控制器类中的紧密耦合数据访问代码,不使用快速连接池,所有这些都与具体问题,到目前为止发布的代码看起来还可以。

Only, you're setting the rno value as 3rd parameter while you've only 2 in your prepared SQL string. 只是,您将rno值设置为第3个参数,而准备好的SQL字符串中只有2个。

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

I'm not sure if this is careless oversimplification or if you have really read the server logs for any indication of the e.printStackTrace() , but this is not right. 我不确定这是不是粗心的过分简化,或者您是否真的阅读过服务器日志中有关e.printStackTrace()任何指示,但这是不对的。 Make sure that the ul.rno is set on parameter index 2. I'd also rather fix that silly e.printStackTrace() which causes the code to continue to run which you obviously don't want to happen. 确保在参数索引2上设置了ul.rno 。我还想修复那个愚蠢的e.printStackTrace() ,它会使代码继续运行,而这显然是您不想发生的。

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

If the SQL was indeed right after all and you indeed don't get an exception, then apparently there's simply no row which matches the WHERE clause. 如果SQL毕竟确实正确,而且您确实没有得到异常,那么显然没有匹配WHERE子句的行。 The PreparedStatement#executeUpdate() returns an int representing the affected rows. PreparedStatement#executeUpdate()返回表示受影响的行的int I suggest to get hold of it and verify if there was really an update: 我建议掌握它并验证是否确实有更新:

int affectedRows = stat.executeUpdate();

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

Also check what value of ul.rno you've set it with. 还要检查设置了ul.rno值。


Update finally you took the little effort to read the server logs for any evidence of the exception. 终于 更新了你的举手之劳读取服务器日志例外的任何证据。 You should understand that exceptions are very important to learn about as they basically are the whole answer to your concrete problem. 您应该理解,异常非常重要 ,因为它们基本上是您具体问题的完整答案。 The one which you retrieved is basically telling that in the following piece of code 您检索的代码基本上是在下面的代码中告诉您的

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

the variable dob is null . 变量dobnull You should be doing a nullcheck: 您应该进行nullcheck:

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

    return bdate;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM