CodeShark CodeShark - 1 year ago 76
Java Question

DefaultTableModel is empty even tho data is queried

I try to put my ResultSet data into a JTable with help of DefaultTableModel class. As a solution I found a solution on Stackoverflow but modified it a bit (not really relevant here):

public static DefaultTableModel buildTableModel(ResultSet rs) {
try {
ResultSetMetaData metaData = rs.getMetaData();
Vector<String> columnNames = new Vector<String>();
int columnCount = metaData.getColumnCount();
for (int column = 1; column <= columnCount;++column) {
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while ( {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount;++columnIndex) {
return new DefaultTableModel(data, columnNames);
} catch(SQLException e) {
return new DefaultTableModel(new Vector<>(), new Vector<>());

While I debugged this code I was able to see that:

  • ResultSet
    has data,

  • ColumnNames
    is populated,

  • RowCount is zero

  • and I think the issue is this line

    It looks like my vector becomes empty because
    returns already no data.

Can someone help me with this or explains where the issue is? When I bind my generated DefaultTableModel to my JTable it is empty (no data is shown).

Answer Source

I just found the issue short time ago: the problem was the fact that the cursor was set to last row because other methods iterated over the same ResultSet already and thus the cursor had to be set to first row again with first-method of ResultSet class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download