为什么要让jtable从数据库中获取数据并删除第一行?

z0qdvdin  于 2021-07-07  发布在  Java
关注(0)|答案(1)|浏览(310)

我正在试图让一个表包含我在其中设置删除条件的数据。。。到,数据返回一行更细,可能删除了一行?我的代码:

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)});
    } 
}

dba5bblo

dba5bblo1#

我得到的数据少了一行

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

读取循环外的第一行,因此它永远不会添加到模型中。
你的逻辑应该是这样的:

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, "لا يوجد مبيعات في هذا الشهر");
}

即检查在遍历resultset之后添加到模型中的行数。
或者,也许你可以用:

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

我不是一个sqlMaven,所以我不确定在一个空的resultset上会返回什么。

相关问题