DEVFYI - Developer Resource - FYI

I have an application that queries a database and retreives the results into a JTable ...

JDBC Interview Questions and Answers


(Continued from previous question...)

I have an application that queries a database and retreives the results into a JTable ...

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:
while ( myRs.next() ) {
Vector newRow =new Vector();

for ( int i=1;i<=numOfCols;i++ )
{
newRow.addElement(myRs.getObject(i));
}
allRows.addElement(newRow);
}
fireTableChanged(null);

newRow stores each row of the resultset and allRows stores all the rows. Are the vectors here the problem?
Is there another way of dealing with the result set that could execute faster?


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.

(Continued on next question...)

Other Interview Questions