h:dataTable unable to display distinct rows. It is showing the same rows even with a List of values passed to it

StackOverflow https://stackoverflow.com/questions/23089372

Question

I have a dataTable which extracts data from the database. When I have to extract a single row of data, my method works fine. But when I have a number of rows, my method doesn't work. It prints the same rows again and again.

Here is my IssuanceBean.java code:

public class IssuanceBean {

public IssuanceBean()
 {
    issueTasks();
 }

 private List<EmployeeDutySchedule> issuanceList=new ArrayList<EmployeeDutySchedule>();

 TaskServices ts=new TaskServices();

 public List issueTasks()
 {
     System.out.println("issueTasks() called");
     setIssuanceList(ts.IssuanceTask());

     return issuanceList;
 }

EmployeeDutySchedule simply plays as an intermediate with getter and setter of certain strings like emp ID, first_name, last_name etc.

My Taskservices.java is the java file where i perform my query and this is the method:

    public List<EmployeeDutySchedule> IssuanceTask()
{
     List<EmployeeDutySchedule> empDutyList=new ArrayList<EmployeeDutySchedule>();
    EmployeeDutySchedule empDuty=new EmployeeDutySchedule();
   ResultSet rs=null;
   String query="Select employee_duty_schedule.emp_id, first_name, last_name,  emp_designation, description, task_allocated_id \n" +
 "From\n" +
 " transport_department_schema.employee_duty_schedule, \n" +
"transport_department_schema.m_subtasks, transport_department_schema.employees_information  WHERE\n" +
" employees_information.emp_id=employee_duty_schedule.emp_id AND\n" +
"employee_duty_schedule.task_allocated_id=m_subtasks.subtask_id\n" +
"AND employee_duty_schedule.task_allocated_id LIKE 'L001%'; ";
    System.out.println(query);
      rs=MyQueryExe.executeQuery(query);


            System.out.println("rs "+rs);
            try
        {
        while(rs.next())
        {
         empDuty.setEmpID(rs.getString("emp_id"));
         empDuty.setFirstName(rs.getString("first_name"));
         empDuty.setLastName(rs.getString("last_name"));
         empDuty.setSubTaskID(rs.getString("emp_id"));
         empDuty.setTaskDescription(rs.getString("emp_id"));
         empDuty.setDesignation(rs.getString("emp_designation"));

         empDutyList.add(empDuty);
            System.out.println("size of list  "+empDutyList.size());

        }
        }
        catch(SQLException e)
        {
            e.getStackTrace();
        }
        return empDutyList;
 }

I have checked my query on the database and it runs perfectly.

Now my xhtml dataTable::

   <h:dataTable value="#{issuanceBean.issuanceList}" var="u" rendered='true'
                     styleClass="infoTable" headerClass="dataTableHeader" rowClasses="dataTableRow">

                     <h:column>
                        <f:facet name="header">
                        Task ID
                        </f:facet>   
                         #{u.subTaskID}
                    </h:column>
                         <h:column>
                        <f:facet name="header">
                        Task Description
                        </f:facet>   
                         #{u.taskDescription}
                    </h:column>
                         <h:column>
                        <f:facet name="header">
                        Employee ID
                        </f:facet>   
                         #{u.empID}
                    </h:column>
                         <h:column>
                        <f:facet name="header">
                        First Name
                        </f:facet>   
                         #{u.firstName}
                    </h:column>

                            <h:column>
                        <f:facet name="header">
                        Last Name
                        </f:facet>   
                         #{u.lastName}
                    </h:column>
                         <h:column>
                        <f:facet name="header">
                        Designation
                        </f:facet>   
                         #{u.designation}
                    </h:column>

                     </h:dataTable>

I am showing a screenshot of the data table that I require [after running the query in db]:

view of the required datatable

![photo][1]:http://imgur.com/oqZsCaJ

Now I am showing the one that I am getting instead:

![photo1][2]:http://imgur.com/dkC6XEn

I am not able to understand how to get the correct rows printed!

Was it helpful?

Solution

You have to put the EmployeeDutySchedule empDuty=new EmployeeDutySchedule(); into the while(rs.next()) {...} part. With the current code, you are updating the same objects attributes, and keep adding the same object. Create new instance for every row in your resultset.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top