[SOLVED] SQL row and count using for loop

Issue

This Content is from Stack Overflow. Question asked by DarkBot

I’m making a query that will return a multiple of rows, I need to find a way to loop through these rows and extract a value from each row.

How can I make a for loop that will start from the first row and end at the last ?

repo = findbyID
for (int i = ? ; i = ? ; ? ) // Id's are not in order and can be somthing like 5,2,10,12...



Solution

Your syntax seems a little bit off. Perhaps you want something like this:

List<Item> items = itemRepository.findAll();
items.forEach(item -> System.out.println(item.getId()));

You could also explicitly specify the order, by convention, in the method name itself, if you’re using Spring Data:

List<Item> items = itemRepository.findAllByOrderByIdAsc();
items.forEach(System.out.println(item -> item.getId()));


This Question was asked in StackOverflow by DarkBot and Answered by Woodchuck It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?