tags:

views:

373

answers:

1

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.

How?

+3  A: 

Loop over the results of you ResultSet and put in into a 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

Pierre
Interesting indent style..
Steve Kuo
this works decently, but it doesn't appear to have any scroll bars or ability to resize columns if there are many.
Maslow
well apparently on a smaller data set, scroll bars and resizing columns works. guess the first table I fed it was excessively wide, possibly long too.
Maslow