Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display a java ResultSet visually?

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?

like image 277
Maslow Avatar asked May 30 '09 22:05

Maslow


People also ask

How do I print ResultSet sizes?

println("Total number of rows in ResultSet object = "+rowCount); Simply iterate on ResultSet object and increment rowCount to obtain size of ResultSet object in java. rowCount = rs. getRow();

How do I get the ResultSet column?

You can get the name of a particular column using the getColumnName() method of the ResultSetMetadata interface. This method accepts an integer value representing the index of a column and returns a String value representing the name of the specified column.

How do you find the ResultSet object?

ResultSet is obtained by calling the executeQuery method on Statement instance. Initially, the cursor of ResultSet points to the position before the first row. The method next of ResultSet moves the cursor to the next row. It returns true if there is further row otherwise it returns false.


1 Answers

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

like image 149
Pierre Avatar answered Sep 20 '22 22:09

Pierre