i need to display the contents of a database(table) MS access db, on to the screen. how do i got abt it??? the data keeps changing dynamically...the program should be able to refresh it self

Dani AI

Generated

Practical, minimal plan that answers the questions raised by and builds on and : back the JTable with a custom TableModel that holds the current rows/columns; fetch the database in a background thread; when the fetched data replaces the model’s contents, fire the appropriate table events so the JTable automatically shows added/removed rows. ’s pointer to the Swing JTable tutorial is a good read for JTable basics, but the key for a dynamic view is a model that you can repopulate and notify.

Implement a small AbstractTableModel that keeps column names and a List of Object[] rows. Provide a refresh(ResultSet rs) method that reads ResultSetMetaData, rebuilds the columns/rows in memory, then calls fireTableStructureChanged() if the column set changed or fireTableDataChanged() if only rows changed. Sample skeleton:

class ResultSetTableModel extends AbstractTableModel {
  List<String> cols = new ArrayList<>();
  List<Object[]> rows = new ArrayList<>();

  public void refresh(ResultSet rs) throws SQLException {
    cols.clear(); rows.clear();
    ResultSetMetaData md = rs.getMetaData();
    int c = md.getColumnCount();
    for (int i=1;i<=c;i++) cols.add(md.getColumnLabel(i));
    while (rs.next()) {
      Object[] r = new Object[c];
      for (int i=0;i<c;i++) r[i] = rs.getObject(i+1);
      rows.add(r);
    }
    fireTableStructureChanged(); // or fireTableDataChanged() if cols same
  }

  public int getRowCount(){ return rows.size(); }
  public int getColumnCount(){ return cols.size(); }
  public Object getValueAt(int r,int c){ return rows.get(r)[c]; }
  public String getColumnName(int c){ return cols.get(c); }
}

Threading and refresh strategy: never do DB I/O on the EDT. Use SwingWorker or a ScheduledExecutorService to poll (or trigger) the fetch, then update the model on the EDT (SwingUtilities.invokeLater or SwingWorker.publish/process). If you expect only row changes, prefer granular events like fireTableRowsInserted/fireTableRowsDeleted to preserve selection and scroll. Close JDBC resources (try-with-resources), limit polling frequency, and—since MS Access is file-based and lacks server push—consider fetching only changed rows (last_modified column) or migrating to a server DB if concurrency/scale becomes an issue.

Troubleshooting notes: log SQL and exceptions, confirm refresh runs and that model notifications execute on the EDT, and use paging or lazy loading for very large result sets.

Recommended Answers

All 5 Replies

Create a JTable, create a custom TableModel for it, and have a Timer or whatever refresh that at regular intervals.

Tablemodel??? wat is that? do u have or know of any simple example that explains it?


Create a JTable, create a custom TableModel for it, and have a Timer or whatever refresh that at regular intervals.

Try to cut up your structure of your program in several little pieces and start with those one by one.

For example: You'll first need to make a connection to a database itself.
If your connection works, you can try to read out your data. (A datareader is recommended).
After this you can start building up your gui (there is quite a lot of documentation on this too, try the java swing components) and how you are going to refresh the data.

Baby steps, baby steps :p.

The making of a connection, reading it and the creation of the gui are well documented on the internet. I suggest that you try to make those step by step. If you have difficulties at any given moment with any given 'module', you can always ask for a specific problem.

I hope this helps a bit, good luck with your program.

thanks for the steps... but i know upto retrieving values from the db...how do i display it in a table? and with the varying number of records in how does the table rows increase or decrease?

Try to cut up your structure of your program in several little pieces and start with those one by one.

For example: You'll first need to make a connection to a database itself.
If your connection works, you can try to read out your data. (A datareader is recommended).
After this you can start building up your gui (there is quite a lot of documentation on this too, try the java swing components) and how you are going to refresh the data.

Baby steps, baby steps :p.

The making of a connection, reading it and the creation of the gui are well documented on the internet. I suggest that you try to make those step by step. If you have difficulties at any given moment with any given 'module', you can always ask for a specific problem.

I hope this helps a bit, good luck with your program.

Start with a simple tutorial on JTable: http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

(These examples that you keep asking for are incredibly easy to locate with a very simple search. You should take a little initiative to find them before asking others to give them to you.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.