Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Displaying SQL table in Java form

Options
  • 12-04-2005 5:36pm
    #1
    Registered Users Posts: 7,498 ✭✭✭


    Hey im trying to display an SQL database table on a java form. I was thinking that i could use a jTable to do this but i dont know how?

    Can anyone tell me how i could go about doing this or if you know a better way to display a database table please tell me.

    Thanks.


Comments

  • Registered Users Posts: 4,003 ✭✭✭rsynnott


    What, a whole one? If yes, then yep, a JTable would work. But DON'T load it all in at once, if it's a big one. JTable isn't designed for it.


  • Registered Users Posts: 7,498 ✭✭✭BrokenArrows


    Its only a table with about 4 colums and 10 rows.

    Ok now that i know that this is what i use how do i load the information into it?


  • Registered Users Posts: 4,188 ✭✭✭pH


    There's no automatic way to build a table from a result set, though I dare say a google would probably turn up one or 2, as it's a fairly common thing to do a utility for.

    The basics in case you need to write it yourself:

    Execute Query and get a resultset
    create a new default table model and table
    JTable table = new JTable();
    DefaultTableModel model = new DefaultTableModel(0,4);

    for each row create a vector or an array of objects with that rows contents
    Object thisRow = new Object[4];
    thisRow[0] = rs.getString("mycol");
    ...
    model.add( thisRow);


    then set the model

    table.setModel(model);

    If you want to get tricky you can automate all this by discovering the number of cols in the result set .


  • Closed Accounts Posts: 92 ✭✭tempest


    Or you could be a little more object oriented and..
    public void aMethod () {
      //Execute Query and get a resultset
      final SQLTableModel model = new SQLTableModel(resultSet);
      final JTable table = new JTable(model);
    }
    
    public class SQLTableModel extends AbstractTableModel {
    
      private final ArrayList columnNames = new ArrayList();
    
      private final ArrayList columns = new ArrayList();
    
      private ResultSet rset = null;
    
      public SQLTableModel(final ResultSet rset) throws SQLException {
          
         ResultSetMetaData meta = rset.getMetaData();
        
         // Get the column names from the meta data and store in the list
    
         while (rset.hasNext) {
               // Create a new list and add it to the list of values.
              // The size could be limited to ~100 rows to prevent long waits.
    
          }
    
          // If you are not worried about open connections / building a scalable app then you could cache the result set and reuse it if required.
    
         this.rset = rset;
      }
    
       //// ..... override getColumnName and getValueAt methods at least
    
    }
    


Advertisement