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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Displaying SQL table in Java form

  • 12-04-2005 4:36pm
    #1
    Registered Users, Registered Users 2 Posts: 7,501 ✭✭✭


    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,501 ✭✭✭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