Skip to content
Advertisement

Query did not return a unique result

This is the query I have written:

select c.id, CONCAT(c.major_version, '.', c.minor_version) as versions
from event_ids c
where c_id in ('101') group by c_id, major_version, minor_version;

This is the output I am getting from the database:

id versions
101 0.0
101 1.0
101 2.0
101 3.0

In my application, I am storing this result in

Map<List<String>, List<String>>

but it gives an error saying “query did not return a unique result”

How can I store this result? which data structure could I use?

Advertisement

Answer

Try the below method:

void test2(ResultSet resultSet) throws SQLException {
        HashMap<String, List<String>> stringListHashMap = new HashMap<>();
        while (resultSet.next()){
            String id = resultSet.getString("id");
            String version = resultSet.getString("versions");
            if (stringListHashMap.containsKey(id)){ // check if the id is already available in the map 
                stringListHashMap.get(id).add(version); //if id is available get the list and add to the current version
            } else {
             // if not available create a new list 
             // add the current version to the list
             // then put the id and list to the map
                    List<String> versions = new ArrayList<>();
                    versions.add(version);
                    stringListHashMap.put(id,versions);
                }
            }
        }
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement