0

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

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
user1924104
  • 891
  • 2
  • 16
  • 38

1 Answers1

5

The error is very explicit:

Cannot convert richard.test.User@129d62a7 of type class richard.test.User to long

You're sending the user object when your edit method receives a long. This is noted here.

JSF code:

<h:commandButton value="Edit" action="#{user.editData(u)}" />

Java code:

public void editData(long userID) {
    //...
}

Solutions:

  1. Change the JSF code to send the user id as you do it in the delete method:

    <h:commandButton value="Delete" action="#{user.delete(u.userID)}" />
    
  2. Change your Java code to receive the user as parameter in your Java method:

    public void editData(User user) {
        //...
    }
    

There are other problems with your current code:

  • You open and close a connection manually. This is a must not in real world applications. Instead you should use a database connection pool for this. There are several ways to accomplish this, you can choose to create it as a JNDI resource in your application server or use a third party library that handles this.
  • You're not using all the power of a PreparedStatement. You're creating the query by concatenating the values, while you should make use of parameters, those things using ? where you want to set a parameter (which is the main purpose of the PreparedStatement).

More info:

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332