Unable to edit a database row from JSF

Posted by user1924104 on Stack Overflow See other posts from Stack Overflow or by user1924104
Published on 2013-10-25T21:50:26Z Indexed on 2013/10/25 21:53 UTC
Read the original article Hit count: 255

Filed under:
|
|
|
|

Hi guys i have a data table in JSF which displays all of the contents of my database table, it displays it fine, i also have a delete function that can successfully delete from the database fine and updates the data table fine however when i try to update the database i get the error

java.lang.IllegalArgumentException: Cannot convert richard.test.User@129d62a7 of type class richard.test.User to long

below is the code that i have been using to delete the rows in the database that is working fine :

  public void delete(long userID) {
        PreparedStatement ps = null;
        Connection con = null;
        if (userID != 0) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
                String sql = "DELETE FROM user1 WHERE userId=" + userID;
                ps = con.prepareStatement(sql);
                int i = ps.executeUpdate();
                if (i > 0) {
                    System.out.println("Row deleted successfully");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    con.close();
                    ps.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

i simply wanted to edit the above code so it would update the records instead of deleting them so i edited it to look like :

public void editData(long userID) {
        PreparedStatement ps = null;
        Connection con = null;
        if (userID != 0) {
            try {
                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
                String sql =  "UPDATE user1 set name = '"+name+"', email = '"+ email +"', address = '"+address+"' WHERE userId=" + userID;
                ps = con.prepareStatement(sql);
                int i = ps.executeUpdate();
                if (i > 0) {
                    System.out.println("Row updated successfully");
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    con.close();
                    ps.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

and the xhmtl is :

            <p:dataTable id="dataTable" var="u" value="#{userBean.getUserList()}"  
                         paginator="true" rows="10"  
                         editable="true"
                         paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                         rowsPerPageTemplate="5,10,15">  



                <p:column>

                    <f:facet name="header">
                        User ID
                    </f:facet>
                    #{u.userID}

                </p:column>

                <p:column>
                    <f:facet name="header">
                        Name
                    </f:facet>
                    #{u.name}
                </p:column>

                <p:column>
                    <f:facet name="header">
                        Email 
                    </f:facet>
                    #{u.email}
                </p:column>
                <p:column>
                    <f:facet name="header">
                        Address
                    </f:facet>
                    #{u.address}
                </p:column>

                <p:column>
                    <f:facet name="header">
                        Created Date
                    </f:facet>
                    #{u.created_date}
                </p:column>

                <p:column>
                    <f:facet name="header">
                        Delete
                    </f:facet>
                    <h:commandButton value="Delete" action="#{user.delete(u.userID)}" />
                </p:column>
                <p:column>
                    <f:facet name="header">
                        Delete
                    </f:facet>
                    <h:commandButton value="Edit" action="#{user.editData(u)}" />
                </p:column>

currently when you press the edit button it will only update it with the same values as i haven't yet managed to get the datatable to be editable with the database, i have seen a few examples with an array list where the data table gets its values from but never a database so if you have any advice on this too it would be great

thanks

© Stack Overflow or respective owner

Related posts about mysql

Related posts about sql