Heidi Lilybeth
Heidi Lilybeth

Reputation: 127

JSF display database records on datatable

I've been searching here for hour now and unfortunately I can't how to display database records to datatable, I'm newbie to JSF and I don't know much on JSF right now but I'm building a simple crud application I already know how to create, delete records using JSF but I'm having problem displaying this records to my datatable. I tried creating arraylist, I tried creating another class for this, To make it more clear here is my code:

This is my index.jsf:

<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<f:view xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html">
    <html xmlns="http://www.w3.org/1999/xhtml">
        <h:head></h:head>
        <h:body>
            Username: 
            <h:outputText value="#{ backing_index.userName }" id="Username">
                <p>
                    &nbsp;
                </p>
                <p>
                    &nbsp;
                </p>
                <p>
                    &nbsp;
                </p>
            </h:outputText>
            <p>
                RoleI.D: 
                <h:outputText value="#{backing_index.roleId}" id="RoleID"/>
            </p>
            Role Description: 
            <h:outputText  value="#{backing_index.roleDesc}" id="Description"/>

            <h:dataTable value="#{ backing_index.tableRs }" var="user" rules="rows" cellpadding="7">
                <f:facet name="header"></f:facet>
                <f:facet name="footer"></f:facet>

                <h:column>
                    <f:facet name="header">ID</f:facet>
                    #{ user.tableId }
                </h:column>

                <h:column>
                    <f:facet name="header">First Name</f:facet>
                     #{ user.tableFirstName }
                </h:column>

                <h:column>
                    <f:facet name="header">Middle Name</f:facet>
                     #{ user.tableMiddleName }
                </h:column>

                <h:column>
                    <f:facet name="header">Last Name</f:facet>
                     #{ user.tableLastName }
                </h:column>

                <h:column>
                    <f:facet name="header">Delete</f:facet>
                    <h:commandButton action="#{ backing_index.deleteAction }" value="Remove this">
                        <f:param value="Remove" name="delete" />
                    </h:commandButton>
                </h:column>

            </h:dataTable>
        </h:body>
    </html>
    <!--oracle-jdev-comment:auto-binding-backing-bean-name:backing_index-->
</f:view>

Here is my code for the bean:

package view.backing;

import javax.faces.component.html.HtmlOutputText;
import javax.faces.bean.*;
import javax.faces.context.*;
import javax.annotation.*;
import javax.faces.*;
import java.sql.*;
import java.util.*;

@RequestScoped

public class Index {

private Connection con;
private ResultSet rs;

private String userName;
private String roleId;
private String roleDesc;

//Variable of Data Table
private TableUser[] tableRs;
//End of Variable


//Start of getter and setter for Data table

public void setTableRs(Index.TableUser[] tableRs) {
    this.tableRs = tableRs;
}

public Index.TableUser[] getTableRs() {
    return tableRs;
}

//End of getter and setter


public void setUserName(String userName) {
    this.userName = userName;
}

public String getUserName() {
    return userName;
}

public void setRoleId(String roleId) {
    this.roleId = roleId;
}

public String getRoleId() {
    return roleId;
}

public void setRoleDesc(String roleDesc) {
    this.roleDesc = roleDesc;
}

public String getRoleDesc() {
    return roleDesc;
}

@PostConstruct
public void init()throws SQLException, ClassNotFoundException{

    Class.forName("oracle.jdbc.driver.OracleDriver");
    con = DriverManager.getConnection("jdbc:oracle:thin:@localhost/XE", "JEROME", "perbert101");
    displayUserInfo();
    displayTableRecords();
}

private void displayUserInfo()throws SQLException{
    FacesContext context = FacesContext.getCurrentInstance();
    userName = (String)context.getExternalContext().getSessionMap().get("userName");
    roleId = (String)context.getExternalContext().getSessionMap().get("roleId");

    Statement state = con.createStatement();
    state.executeQuery("SELECT * FROM ROLES WHERE ID = 2");
    rs = state.getResultSet();
    while(rs.next()){
        roleDesc = rs.getString(3);
    }

}

private void displayTableRecords()throws SQLException{
    String query = "SELECT * FROM USERS";
    PreparedStatement state = con.prepareStatement(query);
    state.execute();
    rs = state.getResultSet();
    while(rs.next()){
        tableRs = new TableUser[]{new TableUser(rs.getLong(1), rs.getString(2), rs.getString(7), rs.getString(5))};

    }

}

//Table Records Store
public static class TableUser{
    long tableId;
    String tableFirstName;
    String tableMiddleName;
    String tableLastName;

    public TableUser(long tableId, String tableFirstName, String tableMiddleName, String tableLastName){
        this.tableId = tableId;
        this.tableFirstName = tableFirstName;
        this.tableMiddleName = tableMiddleName;
        this.tableLastName = tableLastName;
    }

    public void setTableId(long tableId) {
        this.tableId = tableId;
    }

    public long getTableId() {
        return tableId;
    }

    public void setTableFirstName(String tableFirstName) {
        this.tableFirstName = tableFirstName;
    }

    public String getTableFirstName() {
        return tableFirstName;
    }

    public void setTableMiddleName(String tableMiddleName) {
        this.tableMiddleName = tableMiddleName;
    }

    public String getTableMiddleName() {
        return tableMiddleName;
    }

    public void setTableLastName(String tableLastName) {
        this.tableLastName = tableLastName;
    }

    public String getTableLastName() {
        return tableLastName;
    }
}

}

I don't have any error or something and it display only the last records in the database. Guys if you know the easiest ways can you teach me how to do it, and I always go for a nice clean, short code. your help is really much appreciated :)

Upvotes: 1

Views: 8681

Answers (1)

ravindra
ravindra

Reputation: 186

There is a bug in your displayTableRecords() method. Within while loop you instantiate new TableUser array for each iteration. Actually what you should do is add TableUser object one by one to existing array.

Use ArrayList inseadof array.

private List<TableUser> tableRs = new ArrayList<TableUser>();

public List<TableUser> getTableRs() {
    return tableRs;
}

public void setTableRs(List<TableUser> tableRs) {
    this.tableRs = tableRs;
}

private void displayTableRecords() {
    String query = "SELECT * FROM USERS";
    PreparedStatement state = con.prepareStatement(query);
    state.execute();
    rs = state.getResultSet();
    while (rs.next()) {

        tableRs.add(new TableUser(rs.getLong(1),
                rs.getString(2), rs.getString(7), rs.getString(5)));

    }
}

Upvotes: 3

Related Questions