java 从数据库检索到 JLabel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17341223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Retrieve from database into JLabel
提问by Ani
I want to retrieve data from database and display into JLabels .I am working in Netbeans and i have done this..
我想从数据库中检索数据并显示到 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);}
}
Its unable to display anything.
I tried this in notepad and its working fine but not in netbeans.
Any help is appreciated.
它无法显示任何内容。我在记事本中试过这个,它工作正常,但在 netbeans 中没有。
任何帮助表示赞赏。
回答by PermGenError
you should call ResultSet#next()before getting the column information.
您应该在获取列信息之前调用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"));
}
I strongly recommend you to use PreparedStatement instead of Simple Statement which would make your code prone to SQL Injection
我强烈建议您使用 PreparedStatement 而不是 Simple Statement 这会使您的代码容易受到 SQL 注入
回答by shreyansh jogi
try this
试试这个
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"}))