0

Can you populate more than one jTable with the same resultSet?

public void tableDisplay() {

    String tableQuery = "SELECT foodQuantity,foodName FROM food ORDER BY RAND() LIMIT 3";
    ResultSet rs;
    PreparedStatement statement;
    try {

        statement = con.prepareStatement(tableQuery);
        rs = statement.executeQuery();
        jTable1.setModel(DbUtils.resultSetToTableModel(rs));
        jTable2.setModel(DbUtils.resultSetToTableModel(rs));

    } catch (SQLException ex) {
        System.out.println(ex.toString());
    }
}

The code compiles but the second table doesn't get any records from DB. The point is that I need to select random items from mySql table and I want to display them in few jTables.

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Lets have a look at DbUtils.resultSetToTableModel - if that iterates over the RS each time it's called, you're on to a loser ( unless you force it back to the beginning of the ResultSet each time ) – DaveH Mar 25 '14 at 16:42
  • 1
    `TableModel model = DbUtils.resultSetToTableModel(rs); ;jTable1.setModel(model); jTable2.setModel(model);` – nachokk Mar 25 '14 at 16:48
  • Or, simply change the one line to read: `jTable2.setModel(jTable1.getModel());` – splungebob Mar 25 '14 at 16:57
  • The solution where you set the TableModel and pass it on as a parameter for each jTable2.setModel(model) method call works and it populates each jTable with the data. Thank you very much for that. However my Result Set is always the same instead of producing random values. Here is my sql Statement "SELECT foodQuantity,foodName FROM food ORDER BY RAND() LIMIT 3"; and I'm trying to use "ORDER BY RAND() function, but it seems as if once the result set is produced it get saved and it's re used any ideas? – user3058242 Mar 25 '14 at 17:01
  • If you want different contents in the tables then you'll need to execute the query twice and populate each model separately. jTable1.setModel(DbUtils.resultSetToTableModel(statement.executeQuery())); jTable2.setModel(DbUtils.resultSetToTableModel(statement.executeQuery())); – DaveH Mar 25 '14 at 17:51
  • Yeah thank you, I've figured that out already :) but thanks anyway – user3058242 Mar 25 '14 at 19:31
  • populate more than one jTable with the same resultSet? share one model for two JTables, – mKorbel Mar 25 '14 at 21:13

2 Answers2

3

Without knowing too much about your code, I'd say that you need to call DbUtils.resultSetToTableModel(rs) once, and store the resulting table model in a local variable. Then, pass that local variable to the two setModel(...) methods

DaveH
  • 7,187
  • 5
  • 32
  • 53
1

How I populate a JTable with resultSet

try{
playerTableModel = (DefaultTableModel)playerTable.getModel();
            rs = controller.getPlayer();
            while (playerTableModel.getRowCount() > 0);
            int columns = playerTableModel.getColumnCount();
            Object[] rows = new Object[columns];
            while(rs.next()){

                    rows[0] = rs.getString(1);
                    rows[1] = rs.getString(2);
                    rows[2] = rs.getString(3);
                    rows[3] = rs.getString(4);

                    playerTableModel.addRow(rows);
}catch(Exception e){
                e.printStackTrace();
        }

Can't you just call same method for the second table too?

Mnemonics
  • 679
  • 1
  • 10
  • 26