I have an application that queries a database and retreives the results into a JTable. This is the code in the model that seems to be taken forever to execute, especially for a large result set:

Dermot Hennessy

A couple of points for you:

java.util.Vector is largely thread safe, which means that there is a greater overhead in calling addElement() as it is a synchronized method. If your result set is very large, and threading is not an issue, you could use one of the thread-unsafe collections in Java 2 to save some time. java.util.ArrayList is the likeliest candidate here.

Do not use a DefaultTableModel as it loads all of your data into memory at once, which will obviously cause a large overhead - instead, use an AbstractTableModel and provide an implementation which only loads data on demand, i.e. when (if) the user scrolls down through the table.

Joe Sam Shirah adds: For another view on the best collection class, see Which is the preferred collection class to use for storing database result sets? Either way, Dermot's point on speed issues with Vectors is certainly valid.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact