0

I would like to delete a dataTable row which is connected to Mysql database. but when I click delete button which is inside of dataTable, nothing happening (no error, the row is not getting deleted) I tried several methods on the internet but nothing happens, same thing no error neither no result. and I would like to mention I'm new to JSF. i referenced below code from this page http://www.devmanuals.com/tutorials/java/jsf/database/deleteData.html
here is my code,

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>

here is my managedbean code,

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);
        }
    }

}

Thanks in advance !!

Abraham
  • 1
  • 1
  • 3
    Well if you don't tell your components what to do after the delete action nothing will happen. Since it is a delete operation you can add in your commandButton the `update` either for your form or your table. Like `` also your form needs to be a `` in order to be updated. So change it to `` – Jorge Campos Feb 14 '18 at 16:26
  • 2
    There are more than 1 thing wrong in your code. Better start with a modern tutorial. Your button won't work outside a datatable either. – Kukeltje Feb 14 '18 at 16:32
  • Start with adding a 'sysout' in the updateDate (bad method name btw) and see how often it is called: https://stackoverflow.com/questions/2090033/why-jsf-calls-getters-multiple-times – Kukeltje Feb 14 '18 at 16:34
  • 1
    Possible duplicate of [commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated](https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value) – Kukeltje Feb 14 '18 at 16:35
  • Summary: ClassNames in java start with a capital, doing database stuff directly in a ManagedBean is bad practice, JPA exists for ages, And the PrimeFaces showcase has a good working example of this... And learn about the existence of OmniFaces and please read all SO Q/A with more than 35 votes. – Kukeltje Feb 14 '18 at 16:39
  • @Kukeltje "doing database stuff directly in a ManagedBean". The class does not even seem to be a managed bean... – Jasper de Vries Feb 14 '18 at 16:46
  • @JasperdeVries: correct, the 'feeding.java' in front of it is misleading but If even that was not working correctly, the datatable would not be populated. And yes, 'assumption is the mother of all....' but in this case... ;-) – Kukeltje Feb 14 '18 at 16:49
  • thanks for reply guys, yeah i'm using getters and setters & database staff in the same java file. datatable is populated correctly but the only problem i'm facing is that i couldn't delete the row. again i'm saying i'm new to jsf. this is my first application – Abraham Feb 14 '18 at 17:46

0 Answers0