Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Displaying SQL table in Java form

  • 12-04-2005 05:36PM
    #1
    Registered Users, Registered Users 2 Posts: 7,544 ✭✭✭


    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, Registered Users 2 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, Registered Users 2 Posts: 7,544 ✭✭✭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, Registered Users 2 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