如何直观地显示 java ResultSet?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/930745/
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
How do I display a java ResultSet visually?
提问by Maslow
I'm looking for a way to display a java.sql.ResultSet on the screen. preferably built-into java or swing. If neither of those have a decent method that's simple, I'd consider spring.
我正在寻找一种在屏幕上显示 java.sql.ResultSet 的方法。最好内置于java或swing中。如果这两者都没有简单的体面方法,我会考虑使用 spring。
How?
如何?
回答by Pierre
Loop over the results of you ResultSet and put in into a TableModel.
循环遍历 ResultSet 的结果并放入 TableModel。
DefaultTableModel resultSetToTableModel(
DefaultTableModel model,
ResultSet row) throws SQLException
{
ResultSetMetaData meta= row.getMetaData();
if(model==null) model= new DefaultTableModel();
String cols[]=new String[meta.getColumnCount()];
for(int i=0;i< cols.length;++i)
{
cols[i]= meta.getColumnLabel(i+1);
}
model.setColumnIdentifiers(cols);
while(row.next())
{
Object data[]= new Object[cols.length];
for(int i=0;i< data.length;++i)
{
data[i]=row.getObject(i+1);
}
model.addRow(data);
}
return model;
}
Then you can do something like
然后你可以做类似的事情
JOptionPane.showMessageDialog(null,new JScrollPane(new JTable(model)));
Hopt it helps
跳它有帮助

