简体   繁体   中英

How to use jsf to edit some data in a database

i have an app that displays some data entered in a databse, this should allow a user to delete or edit this information, so far i have managed to get it to delete, but i am wondering, what is the best method for editting the data, what i would like if possible is to click on an edit button on a page already populated with the data and this to take the user to a webpage that has only that selected records pre populating textboxs and then allow the user to edit these, how would i do this ?

this is how i am deleting a row from the database

                <p:dataTable id="dataTable" var="u" value="#{userBean.getUserList()}"  
                             paginator="true" rows="10"  
                             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">
                            Edit
                        </f:facet>
                        <h:commandButton value="Edit" action="{user.delete(u.userID)}" />
                    </p:column>
                </p:dataTable>

and this is the backing bean allowing me to delete

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

Thanks

EDIT

i have figured out how to update the data, my question is now simply, how do i get the data from a row and populate a new page with input boxes with the selected data Or even better would be to edit the data live in the table, is this possilbe

instead of taking the user to a second page to edit the data, you can make use of primfaces DataTable - Row Editing see this

http://www.primefaces.org/showcase/ui/datatableRowEditing.jsf

send user object only with editdata method

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

then you can use prepared statement with update like shown below

String sql = "UPDATE user1 set name = '"+name+"', email = '"+ email +"', address = '"+address+"' WHERE userId=" + userID;

I will suggest try hibernate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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