1

I have a JTable that uses ArrayList to load data from a database, but I have an issue that the same data keeps repeating themselves. What Am I missing here ? thanks in advance,

    private void AllTicketRecords() {
    try {
        TicketRecordDao ticketrecdao = new TicketRecordDao();

        ArrayList<TicketRecord> records = ticketrecdao.getAllTicketsRecords();


        Object[] tableColumnName = new Object[7];

        tableColumnName[0] = "Date";
        tableColumnName[1] = "H.License";
        tableColumnName[2] = "First Name";
        tableColumnName[3] = "Last Name";
        tableColumnName[4] = "Ticket ID";
        tableColumnName[5] = "Violation";
        tableColumnName[6] = "Cost";

        DefaultTableModel tbd = new DefaultTableModel();

        tbd.setColumnIdentifiers(tableColumnName);

        this.TicketsSummaryTable.setModel(tbd);

        Object[] RowRec = new Object[7];

        for (int i = 0; i < records.size(); i++) {

            RowRec[0] = records.get(i).getDATE();
            RowRec[1] = records.get(i).getEMP_ID();
            RowRec[2] = records.get(i).getFNAME().toUpperCase();
            RowRec[3] = records.get(i).getLNAME().toUpperCase();
            RowRec[4] = records.get(i).getTICKET_ID();
            RowRec[5] = records.get(i).getVIOLATION();
            RowRec[6] = nf.format(records.get(i).getCOST());

            tbd.addRow(RowRec);

        }
    } catch (SQLException e) {
        System.out.println(e.toString());
    }

}


public ArrayList<TicketRecord> getAllTicketsRecords() throws SQLException {

    String sql = "SELECT DATE, T.EMP_ID ,E.FNAME, E.LNAME ,TICKET_ID, VIOLATION, COST"
            + " FROM EMPLOYEE E INNER JOIN TICKET T ON E.EMP_ID = E.EMP_ID"
            + " ORDER BY DATE";
    DBConnection con = new DBConnection();
    Connection connect = con.getConnection();
    Statement stm = null;
    ArrayList<TicketRecord> records = new ArrayList<>();

    try {
        stm = connect.createStatement();
        ResultSet rs = stm.executeQuery(sql);

        while (rs.next()) {

            TicketRecord tickterec = new TicketRecord();

            tickterec.setDATE(rs.getString("DATE"));
            tickterec.setEMP_ID(rs.getInt("EMP_ID"));
            tickterec.setFNAME(rs.getString("FNAME"));
            tickterec.setLNAME(rs.getString("LNAME"));
            tickterec.setTICKET_ID(rs.getString("TICKET_ID"));
            tickterec.setVIOLATION(rs.getString("VIOLATION"));
            tickterec.setCOST(rs.getDouble("COST"));

            records.add(tickterec);

        }

    } catch (SQLException e) {
        System.out.println("" + e.toString());

    } finally {
        if (connect != null) {
            connect.close();
        }
        if (stm != null) {
            stm.close();
        }
    }
    return records;

}

enter image description here

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
devjaouad
  • 37
  • 6
  • Did you get a different result when you execute your SQL statement other SQL client application? – owenrb Aug 25 '15 at 00:51
  • http://stackoverflow.com/questions/28995533/duplicate-array-list-entries , http://stackoverflow.com/questions/7080546/add-an-object-to-an-arraylist-and-modify-it-later?lq=1 – user2864740 Aug 25 '15 at 00:56

1 Answers1

2

The main cause of your problem seems to be related to...

Object[] RowRec = new Object[7];
for (int i = 0; i < records.size(); i++) {

    RowRec[0] = records.get(i).getDATE();

Basically, you're using the same instance of RowRec, but are simply changing the elements of the array, which will effect ALL of the other previously added rows.

Instead, create a new instance of RowRec for each row...

for (int i = 0; i < records.size(); i++) {
    Object[] RowRec = new Object[7];
    RowRec[0] = records.get(i).getDATE();

I'd also verify your database results to make sure there aren't any duplicates just to be sure ;)

On a side node, I would simply create a custom TableModel, extending from something like AbstractTableModel, which could use the TicketRecord directly, rather then decomposing the existing record values for the DefaultTableModel, but that's me

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • I did create a new instance inside the for loop but I got the same result as before I dont know what i am missing, I have the same code under search button and it works fine, when I type ticket id/emp id it gives me only the record i want, no duplicates – devjaouad Aug 25 '15 at 01:16
  • Dump the results from your query, it may be possible that the query is actually returning duplicates – MadProgrammer Aug 25 '15 at 01:17
  • Thank u, I got it works fine now. the problem was in SQL statement – devjaouad Aug 25 '15 at 01:52