我想从数据库中检索数据并显示到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中 任何帮助表示赞赏。
答案 0 :(得分: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注入
答案 1 :(得分: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"}))