How can i get all the values from all the columns?
Ive tried with this code:
values.add(rs.getString(number));
Where number is the rowcount.
But it only gives me all the values from the last column.
I need to grab the values from every column and then add it to the arraylist.
This is my full code:
// The column count starts from 1
int number = 0;
for ( i = 1; i < columnCount + 1; i++ ) {
number++;
ColumnNames = rsmd.getColumnName(i);
ar.add(ColumnNames);
System.out.println(ar);
}
model.setColumnCount(columnCount);
while ( rs.next() ) {
// values.add(rs.getString(ar.indexOf(i)));
values.add(rs.getString(number));
System.out.println(values);
model.addRow(new Object[] {value1, value2, value3, value4});
}
ResultsetMetaData holds your column count too. The snippet below will fill out an Object array for every column in a resultset.
The API doc is your friend: http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSetMetaData.html
ResultSet resultSet = getResultSetFromSomewhere();
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
final int columnCount = resultSetMetaData.getColumnCount();
while (resultSet.next()) {
Object[] values = new Object[columnCount];
for (int i = 1; i <= columnCount; i++) {
values[i - 1] = resultSet.getObject(i);
}
model.addRow(values);
}