16

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});

  }
Looptech
  • 213
  • 2
  • 4
  • 12
  • 2
    Never post your question twice, instead improve your old one. – LionC Dec 12 '13 at 14:15
  • 1
    I deleted my previous one and made this the official – Looptech Dec 12 '13 at 14:15
  • 2
    @LionC: true, but never post two comments in a row. edit your old one! :) – Willi Mentzel Dec 12 '13 at 14:16
  • @haywire Snap! ^^ The first one is actually auto-created by the flag, I did not know if it is possible / practiced to edit it (as it is a standard format that represents a certain action) – LionC Dec 12 '13 at 14:18
  • 1
    @Looptech: This is too much code. Give us a short, self containend and correct example (http://sscce.org/). Break it down to your key problem! Is it about your SQL query or is it about your JTable? This is not obvious on the first look. – Willi Mentzel Dec 12 '13 at 14:20
  • Read this: http://docs.oracle.com/javase/6/docs/api/java/sql/ResultSet.html – DwB Dec 12 '13 at 14:24
  • @haywire ok i shortened it down now – Looptech Dec 12 '13 at 14:25

2 Answers2

22

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);
}
Simon Sobisch
  • 6,263
  • 1
  • 18
  • 38
lscoughlin
  • 2,327
  • 16
  • 23
9

For every rs.next():

Object[] row = new Object[columnCount];
for (int i = 1; i <= columnCount; ++i) {
    row[i - 1] = rs.getString(i); // Or even rs.getObject()
}
model.addRow(row);
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • It is working though im not getting the last column in every table i try to select. – Looptech Dec 12 '13 at 14:22
  • I fixed the problem by adding +1 to columnCount in the for loop. – Looptech Dec 12 '13 at 14:30
  • though now in some tables the last value on the first row disappears? – Looptech Dec 12 '13 at 14:33
  • 1
    most of JDBC is (bizzarely) 1 based rather then 0 based ( like arrays ) so you have to pay attention to your looping constructs. – lscoughlin Dec 12 '13 at 14:36
  • Yeah ok, sorry for all the noob questions. This is an introductory course assignment and i have never ever programmed in java Before so i apologize. – Looptech Dec 12 '13 at 14:43
  • @Looptech I erred using `<` instead of `<=`, because of the one-basedness of columns in JDBC. – Joop Eggen Dec 12 '13 at 14:56
  • Yes it's working correct but still in some tables i still get empty values on the last column in the first row. – Looptech Dec 12 '13 at 15:03
  • @Looptech are the data from the first row correct in the DB? For instance text import (CSV) into the DB can be hampered for the first row when the file start with a BOM char to mark Unicode (a zero-width space). – Joop Eggen Dec 12 '13 at 17:26
  • @Joop Eggen Yes it is since it worked before doing this, the interesting thing though is that if i delete that empty row through the program, then suddenly the first row of another table is missing it's last value? – Looptech Dec 12 '13 at 17:44