2

我想从数据库中检索数据并显示到 JLabels 中。我在 Netbeans 工作,我已经这样做了。

private void showActionPerformed(java.awt.event.ActionEvent evt) {                                     
  int row=jTable1.getSelectedRow();
  int column=jTable1.getSelectedColumn();
    String str=(String)jTable1.getValueAt(row,column); 

    try{
    rs=stat.executeQuery("select * from table3 where e_id='"+str+"'");

    jLabel1.setText(rs.getString("name"));
    jLabel12.setText(rs.getString("e_id"));

    }
    catch(Exception e)
    {System.out.print(e);}


}   

它无法显示任何内容。我在记事本中试过这个,它工作正常,但在 netbeans 中没有。
任何帮助表示赞赏。

4

2 回答 2

4

您应该在获取列信息之前调用ResultSet#next() 。

 rs=stat.executeQuery("select * from table3 where e_id='"+str+"'");
    while(rs.next()){
    jLabel1.setText(rs.getString("name"));
    jLabel12.setText(rs.getString("e_id"));
    }

我强烈建议您使用 PreparedStatement 而不是 Simple Statement,这会使您的代码容易发生 SQL 注入

于 2013-06-27T10:58:49.643 回答
0

尝试这个

while (rs1.next()) {
    k= new Object[8][8]; 
    k[i][0] = rs1.getString("st_id");
    k[i][1] = rs1.getString("name");
    k[i][2] = rs1.getString("adm_year");
    k[i][3] = rs1.getString("crrt_year");
    k[i][4] = rs1.getString("mcode");
    k[i][5] = rs1.getString("grade");
    k[i][6] = rs1.getString("credit");
    k[i][7] = rs1.getString("attempt");
    System.out.println(k[i][4]);
    i++;

    }
    jTable1.setModel(new DefaultTableModel(k, new String[]{"Student ID", "Name", "Admmission Year", "Current Year", "Modules", "Grade", "Credit", "Attempt"}))
于 2013-06-27T10:56:15.733 回答