0
votes

Neo4j cypher query collect() return Array in result. In order to iterate it we need it to add in arrayList. The previous process we used is not helping us and throwing an exception.

PREVIOUS CODE:-

public  String GettingCurrentDate() {

    Connection connect = null;
    String query=null;
    try {
        connect = graphdbConnect();
        Statement stmt = connect.createStatement();

                query="match(n:learner) "
                        + " return collect(n.name) as ids";
                System.out.println(query);

                ResultSet rs = stmt.executeQuery(query.toLowerCase());

                while(rs.next()){

                    Array idsList=rs.getArray("ids");

                        System.out.println("idsList :: "+idsList);

                        ArrayList<String> userIds = new ArrayList<>();

                        String[] userIdsArray = (String[])rs.getArray("ids").getArray();

                        for(String id : userIdsArray) {
                            userIds.add(id);
                            System.out.println(userIds+"------userId");
                        }
                }
    }
    catch(Exception e) {
        e.printStackTrace();
    } finally {
        if(connect!=null) {
            try {
                connect.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
    return "sucess";
}

This code is getting exception java.sql.SQLFeatureNotSupportedException: get array

Question:- HOW WILL WE GET THE DATA FROM COLLECT() function and iterate it

2

2 Answers

1
votes

Have you checked to see the actual object returned by

resultSet.getObject("ids")

If it's like user defined procedures, collections in Cypher get returned as ArrayLists, so try casting the returned object to an ArrayList and see if that works for you.

0
votes

Cast Object to ArrayList

                    Object idsList=rs.getObject("ids");
                    System.out.println("idsList :: "+idsList);
                    ArrayList<String> userIds = (ArrayList<String>) idsList;
                    System.out.println("List2 Value: "+userIds);