簡體   English   中英

跳過結果集中的第一行以顯示JTABLE

[英]skip first row in resultset to JTABLE display

我想在使用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);
}

擺脫while循環,您將在每次迭代時重新設置JTable的表模型,並刪除在先前迭代中添加的任何信息。 相反,簡單地使用ResultSet和DbUtils.resultSetToTableModel設置模型一次似乎更有意義。 請注意,我從未使用過該實用程序,但似乎它必須以這種方式工作。 例如,

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)結果集中的所有行。 問題是,您已經通過rs.next()調用消耗了第一行。

也就是說,為什么模型中缺少該行。

我的使用以下代碼:

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