java - ArrayList repeats the same data from a database? -
this question has answer here:
- duplicate objects added list [duplicate] 4 answers
i have jtable uses arraylist load data database, have issue same data keeps repeating themselves. missing here ? 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]; (int = 0; < 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" + " employee e inner join ticket t on e.emp_id = e.emp_id" + " order 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()); } { if (connect != null) { connect.close(); } if (stm != null) { stm.close(); } } return records; }
the main cause of problem seems related to...
object[] rowrec = new object[7]; (int = 0; < records.size(); i++) { rowrec[0] = records.get(i).getdate();
basically, you're using same instance of rowrec
, changing elements of array, effect of other added rows.
instead, create new instance of rowrec
each row...
for (int = 0; < records.size(); i++) { object[] rowrec = new object[7]; rowrec[0] = records.get(i).getdate();
i'd verify database results make sure there aren't duplicates sure ;)
on side node, create custom tablemodel
, extending abstracttablemodel
, use ticketrecord
directly, rather decomposing existing record values defaulttablemodel
, that's me
Comments
Post a Comment