neo4j: A simple example using the JDBC driver

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:

<dependencies>
...
    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-jdbc</artifactId>
        <version>1.9</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>neo4j-maven</id>
        <name>neo4j maven</name>
        <url>http://m2.neo4j.org</url>
    </repository>
</repositories>

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.(Driver.java:52)
at org.neo4j.jdbc.Driver.(Driver.java:43)
at com.centrica.bigquery.JDBCTest.main(JDBCTest.java:17)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException: org.neo4j.cypherdsl.grammar.Execute
at java.net.URLClassLoader 1.run(URLClassLoader.java:366)atjava.net.URLClassLoader 1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
… 8 more
It turns out we also need to add the following dependency:

    <dependency>
        <groupId>org.neo4j</groupId>
        <artifactId>neo4j-cypher-dsl</artifactId>
        <version>1.9</version>
    </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 dataLoad = new ArrayList();
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(resultSet.next()) {
Map

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值