Database Reference
In-Depth Information
Java code. But what about integrating Cypher with your custom application implemented
in Java? Not to worry—the Neo4j Core Java API includes a Cypher API, making it easy to
run Cypher queries from your Java code.
Executing Cypher from Java code
It's easy to query relational databases from Java using JDBC drivers and the JDBC API.
Obtain the connection, create and execute the statement, iterate through the result set, and
you have JDBC database queries in no time. You'll be pleased to know that executing
Cypher queries in Java is even simpler!
The following code demonstrates the execution of the same sample Cypher query we ex-
ecuted in the Web Admin Console in figure 6.5 , which finds all movies that a particular
user has seen:
As you can see, all you need to do is instantiate Neo4j's ExecutionEngine for Cypher
and pass the valid Cypher query to it , . The ExecutionRes-
ult.toString() method creates the output in the same format as the Neo4j Shell,
which makes it useful for debugging and troubleshooting.
To iterate over Cypher query results in Java, all you have to do is iterate over the Execu-
tionResult object returned by the execute(...) method:
ExecutionResult result = engine.execute(cql);
for(Map<String,Object> row : result){
System.out.println("Row:" + row);
}
Each iterator element contains a single result row, represented as a Java Map , where the
keys are column names and the values are actual result values.
Search WWH ::




Custom Search