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) {
columnNames.add(metaData.getColumnName(column));
}
Vector<Vector<Object>> data = new Vector<Vector<Object>>();
while (rs.next()) {
Vector<Object> vector = new Vector<Object>();
for (int columnIndex = 1; columnIndex <= columnCount;++columnIndex) {
vector.add(rs.getObject(columnIndex));
}
data.add(vector);
}
return new DefaultTableModel(data, columnNames);
} catch(SQLException e) {
e.printStackTrace();
return new DefaultTableModel(new Vector<>(), new Vector<>());
}
}
While I debugged this code I was able to see that:
rs
has data,ColumnNames
is populated,vector.add(rs.getObject(columnIndex));
It looks like my vector becomes empty because getObject()
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).
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.