user2513354
user2513354

Reputation: 1

Populating a JTable from Access Database

I am trying to populate a JTable with data from a database. I've tried so many different ways and it just doesn't want to work. Currently I have a class which returns a resultset with the entire database to my UIClass which then uses the resultset to populate the JTable. Bellow is my method that retrieves the resultset and then populates the JTable.

     private void SetJTable()
  {
 int count = 0;
 String boatclass, senior, under23, junior;

 try
 {
 ResultSet results = object.AllWorldBestTimes();
 DefaultTableModel model = (DefaultTableModel) Times.getModel();

 model.addColumn("Boat Class");
 model.addColumn("Senior");
 model.addColumn("Under 23");
 model.addColumn("Junior");

     while(count<24)
     {
        boatclass = results.getString(1);
        senior = results.getString(2);
        under23 = results.getString(3);
        junior = results.getString(4);

        System.out.println(boatclass + senior + under23 + junior);
        model.insertRow(Times.getRowCount(), new Object[]{boatclass, senior, under23, junior});
        count++;
     }
  Times = new JTable(model);
 }
 catch (SQLException e)
 {
  System.out.println("Ef");
 }
}

Here is the method of retrieving data from the database:

     public ResultSet AllWorldBestTimes() throws SQLException
{
  DatabaseConnection connection = new DatabaseConnection();

  ResultSet result = connection.SelectStatements("SELECT * FROM WorldBestTimes");

  return result;
}

there is no crash so i cannot give the stack trace, nor does it go into the catch statement.

any help would be appreciated!

Upvotes: 0

Views: 379

Answers (1)

BillRobertson42
BillRobertson42

Reputation: 12883

model.insertRow(Times.getRowCount(), ...

Times.getRowCount() is always going to return zero, since it's empty.

Times = new JTable(model);

Reassigns the reference Times, but it doesn't take the old table out of the form or add the new one into the form.

Try this...

model = new DefaultTableModel();

...and this...

model.insertRow(model.getRowCount(), ...

...and this...

Times.setModel(model);

Hard to say if that will do it or not. The code sample isn't exactly SCCE.

Upvotes: 1

Related Questions