java 从查询中填充 JTable 的最简单方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6361671/
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
The easiest way to populate JTable from query
提问by Tomi S
+-------+------+------+
| name | level| score|
+-------+------+------+
| data | data | data |
+-------+------+------+
This is how I would like to show in JTable
.
这就是我想在JTable
.
//SQL
//SQL
static String[][] executeQuery(){
blabla;
rs = stmt.executeQuery(sql);
int i=0;
while(rs.next()){
query[0][i++] = rs.getString("name ");
query[1][i] = rs.getString("level");
query[2][i] = rs.getString("score");
}
return query;
}
//MAIN
JPanel panel = new JPanel();
String[][] getScores = executeQuery();
Object[][] data = getScores ;
String[] columnNames = {"name ","level","score"};
JTable table = new JTable (data, columnNames);
table.setEnabled(false);
panel.add(table);
JOptionPane.showMessageDialog(null, panel);
Result of this is is 3x3 table which is not properly oriented. Dont know why?
My question is: do you have some other example or do you see some errors i made ..
I have only this example but its too complicated for me - beginner:
http://www.rgagnon.com/javadetails/java-0309.html
这样做的结果是 3x3 表,它没有正确定向。不知道为什么?
我的问题是:您有其他示例吗,或者您是否看到我犯的一些错误..
我只有这个示例,但对我来说太复杂了 - 初学者:http:
//www.rgagnon.com/javadetails/java-0309。 html
回答by camickr
The easiest way is to add the data to a DefaultTableModel. See the "Table From Database Example" code found in the Table From Databasearticle.
最简单的方法是将数据添加到 DefaultTableModel。请参阅Table From Database文章中的“Table From Database Example”代码。