简体   繁体   English

跳过结果集中的第一行以显示JTABLE

[英]skip first row in resultset to JTABLE display

i want to display data in jtable when i use dbutil it skips the first row and show the other rows here is the code: i tried to make double loop with a rs.first() it didnt work 我想在使用dbutil时在jtable中显示数据,它会跳过第一行,并在此处显示其他行,这是代码:我试图用rs.first()进行双循环,但没有用

int id=0,quantite=0;
double prix=0.0;
ResultSet rs=null;
String sql = "Select id,prix,quantite from commande where quantite="+Integer.parseInt(tfRecherche.getText());
CommandeDao commande = new CommandeDao();
Statement st = commande.getSt();
try {
    rs= st.executeQuery(sql);
    while(rs.next()){
        if(rs.isFirst()){
            System.out.println("first");
        }
        id= rs.getInt("id");
        quantite = rs.getInt("quantite");
        prix = rs.getDouble("prix");
        tbAffichage.setModel(DbUtils.resultSetToTableModel(rs));
    }
} catch (SQLException ex) {
    Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
}

Get rid of the while loop as with it you are re-setting the JTable's table model each time it iterates, deleting any information added in the previous iterations. 摆脱while循环,您将在每次迭代时重新设置JTable的表模型,并删除在先前迭代中添加的任何信息。 Instead it seems to make more sense to simply use the ResultSet and DbUtils.resultSetToTableModel to set the model once. 相反,简单地使用ResultSet和DbUtils.resultSetToTableModel设置模型一次似乎更有意义。 Note that I've never used this utility, but it seems like it has to work this way. 请注意,我从未使用过该实用程序,但似乎它必须以这种方式工作。 eg, 例如,

int id=0,quantite=0;
double prix=0.0;
ResultSet rs=null;
String sql = "Select id,prix,quantite from commande where quantite="+Integer.parseInt(tfRecherche.getText());
CommandeDao commande = new CommandeDao();
Statement st = commande.getSt();
try {
    rs= st.executeQuery(sql);

    // add this
    tbAffichage.setModel(DbUtils.resultSetToTableModel(rs));

    // and get rid of this...
    // while(rs.next()){
        // ....
    // }

} catch (SQLException ex) {
    Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
} finally {
    // close your connection here if not null, or use try-with resources
}

resultSetToTableModel(rs) consumes all rows in your resultset. resultSetToTableModel(rs)结果集中的所有行。 The problem is, that you already consumed the first row with the rs.next() call. 问题是,您已经通过rs.next()调用消耗了第一行。

That is, why the row is missing in the model. 也就是说,为什么模型中缺少该行。

Mine worked with the following code: 我的使用以下代码:

pst = conn.prepareStatement(sql);
res = pst.executeQuery();

if(res.isBeforeFirst())
{
    jTable1.setModel(DbUtils.resultSetToTableModel(res));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM