I am iterating over an ResultSet
and trying to copy its values in an ArrayList
. The problem is that its traversing only once. But using resultset.getString("Col 1")
to resultset.getString('Col n")
is showing all entries of all columns. Below is the code snippet -
ResultSet resultset = null; ArrayList<String> arrayList = new ArrayList<String>(); int i = 1; while (resultset.next()) { arrayList.add(resultset.getString(i++)); System.out.println(resultset.getString("Col 1")); System.out.println(resultset.getString("Col 2")); System.out.println(resultset.getString("Col n")); }
The only value of ResultSet
getting copied into ArrayList
is for column 1. And then while exits. But I can see the value of all columns. Why?
Iterating the ResultSet To iterate the ResultSet you use its next() method. The next() method returns true if the ResultSet has a next record, and moves the ResultSet to point to the next record. If there were no more records, next() returns false, and you can no longer.
getString(String columnLabel) Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language. Time. getTime(int columnIndex) Retrieves the value of the designated column in the current row of this ResultSet object as a java.
If I've understood your problem correctly, there are two possible problems here:
resultset
is null
- I assume that this can't be the case as if it was you'd get an exception in your while loop and nothing would be output.resultset.getString(i++)
will get columns 1,2,3 and so on from each subsequent row.I think that the second point is probably your problem here.
Lets say you only had 1 row returned, as follows:
Col 1, Col 2, Col 3 A , B, C
Your code as it stands would only get A - it wouldn't get the rest of the columns.
I suggest you change your code as follows:
ResultSet resultset = ...; ArrayList<String> arrayList = new ArrayList<String>(); while (resultset.next()) { int i = 1; while(i <= numberOfColumns) { arrayList.add(resultset.getString(i++)); } System.out.println(resultset.getString("Col 1")); System.out.println(resultset.getString("Col 2")); System.out.println(resultset.getString("Col 3")); System.out.println(resultset.getString("Col n")); }
Edit:
To get the number of columns:
ResultSetMetaData metadata = resultset.getMetaData(); int numberOfColumns = metadata.getColumnCount();
Just for the fun, I'm offering an alternative solution using jOOQ and Java 8. Instead of using jOOQ, you could be using any other API that maps JDBC ResultSet
to List
, such as Spring JDBC or Apache DbUtils, or write your own ResultSetIterator
:
List<Object> list = DSL.using(connection) .fetch("SELECT col1, col2, col3, ...") .stream() .flatMap(r -> Arrays.stream(r.intoArray())) .collect(Collectors.toList());
List<Object> list = DSL.using(connection) .fetch("SELECT col1, col2, col3, ...") .stream() .flatMap(Record::intoStream) .collect(Collectors.toList());
(Disclaimer, I work for the company behind jOOQ)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With