Mark Needham

Thoughts on Software Development

neo4j: A simple example using the JDBC driver

without comments

Michael recently pointed me to the neo4j JDBC driver which he and Rickard have written so I thought I’d try and port the code from my previous post to use that instead of the console.

To start with I added the following dependencies to my POM file:

            <name>neo4j maven</name>

I then tried to create a connection to a local neo4j server instance that I had running on port 7474:

Neo4jConnection connect = new Driver().
  connect("jdbc:neo4j://localhost:7474", new Properties());

which leads to the following exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/neo4j/cypherdsl/grammar/Execute
	at org.neo4j.jdbc.Driver.<init>(
	at org.neo4j.jdbc.Driver.<clinit>(
	at com.centrica.bigquery.JDBCTest.main(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at com.intellij.rt.execution.application.AppMain.main(
Caused by: java.lang.ClassNotFoundException: org.neo4j.cypherdsl.grammar.Execute
	at Method)
	at java.lang.ClassLoader.loadClass(
	at sun.misc.Launcher$AppClassLoader.loadClass(
	at java.lang.ClassLoader.loadClass(
	... 8 more

It turns out we also need to add the following dependency:


We can now run the following code to create some people and then query for one of them:

Neo4jConnection connect = new Driver().
  connect("jdbc:neo4j://localhost:7474", new Properties());
List<String> dataLoad = new ArrayList<String>();
dataLoad.add("CREATE (person1 { personId: 1, started: 1361708546 })");
dataLoad.add("CREATE (person2 { personId: 2, started: 1361708546, left: 1371708646 })");
dataLoad.add("CREATE (company { companyId: 1 })");
connect.createStatement().executeQuery(StringUtils.join(dataLoad, "\n"));
ResultSet resultSet = connect.createStatement().
  executeQuery("START person1 = node:node_auto_index('personId:1') RETURN person1");
if( {
    Map<String, Object> e = (Map<String, Object>) resultSet.getObject("person1");
    System.out.println("personId: " + e.get("personId") + ", started: " + e.get("started"));

which gives the following output:

Connecting to URL http://localhost:7474
Starting the Apache HTTP client
Executing query: START person1 = node:node_auto_index('personId:1') RETURN person1
 with params{}
Starting the Apache HTTP client
personId: 1, started: 1361708546

I haven’t done much more with it but JDBC seems like quite a neat integration point because so many other understand that API.

Written by Mark Needham

June 20th, 2013 at 7:21 am

Posted in neo4j

Tagged with ,