Skip to content
Advertisement

why if i make JTable take data from database delete the first row?

I’m trying to make a Table take the data where I make condition a delete from … to … , the data returns one row thinner, maybe one row deleted? my code:

JScrollPane scrollPane_9 = new JScrollPane();
scrollPane_9.setBounds(10, 105, 1396, 524);
month.add(scrollPane_9);
DefaultTableModel tableModel = new DefaultTableModel();
table_10 = new JTable(tableModel);
scrollPane_9.setViewportView(table_10);
tableModel.addColumn("INVOICE ID ");
tableModel.addColumn("DATE");
tableModel.addColumn("CASH");
tableModel.addColumn("MADA");
tableModel.addColumn("COPON PRISE");
tableModel.addColumn("COPONE NAME");
tableModel.addColumn("SELMAN");
tableModel.addColumn("DESCRIPTION");

String sqlQ = "SELECT `invoiceid`,`date`,`cash`,`payme`, `coponpri`, `copon`,`selman`,`desc` FROM `daay` where DATE between DATE(?) and DATE(?)";
                PreparedStatement pstmt5 = con.prepareStatement(sqlQ);

pstmt5.setDate(1, date8);
pstmt5.setDate(2,date9);
            

ResultSet rs4 =  pstmt5.executeQuery();

if (rs4.next() == false)
{
    JOptionPane.showMessageDialog(null, "لا يوجد مبيعات في هذا الشهر");
}
else 
{
    tableModel.addRow(new Object[]{"","","","","","","",""});
    while(rs4.next()){
        tableModel.addRow(new Object[]{rs4.getInt(1), rs4.getDate(2),rs4.getInt(3),rs4.getInt(4),rs4.getInt(5),rs4.getString(6),rs4.getString(7),rs4.getString(8)});
    } 
}

enter image description here

Advertisement

Answer

the data come to me less thin a one row

if (rs4.next() == false)
    ...

You read the first row outside the loop, so it is never added to the model.

Your logic should be something like:

tableModel.addRow(new Object[]{"","","","","","","",""});
while(rs4.next())
{
    tableModel.addRow(new Object[]{rs4.getInt(1), rs4.getDate(2), rs4.getInt(3), rs4.getInt(4), rs4.getInt(5), rs4.getString(6), rs4.getString(7), rs4.getString(8)});
} 

if (tableModel.getRowCount() == 1)
{
     JOptionPane.showMessageDialog(null, "لا يوجد مبيعات في هذا الشهر");
}

That is check the number of rows added to the model after iterating through the ResultSet.

Or, maybe you could use:

//if (rs4.next() == false)
if (rs4.isLast())

I’m not an SQL expert, so I’m not sure what that will return on an empty ResultSet.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement