Using Neo4j from Java

Important - page not maintained

This page is no longer being maintained and its content may be out of date. For the latest guidance, please visit the Getting Started Manual .

Goals
If you are a Java developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive, it will introduce the different APIs and link to the relevant resources.
Prerequisites
You should be familiar with graph database concepts and the property graph model. You should have created an Neo4j AuraDB cloud instance, or installed Neo4j locally When developing with Neo4j, please use Java 8 or 11 and your favorite IDE.

Intermediate

Neo4j for Java Developers

java

Neo4j provides drivers which allow you to make a connection to the database and develop applications which create, read, update, and delete information from the graph.

For Community Edition and Enterprise Edition, you can also extend Neo4j by implementing user defined procedures for Cypher in Java or other JVM languages.

The Example Project

The Neo4j example project is a small, one page webapp for the movies database built into the Neo4j tutorial. The front-end page is the same for all drivers: movie search, movie details, and a graph visualization of actors and movies. Each backend implementation shows you how to connect to Neo4j from each of the different languages and drivers.

You can learn more about our small, consistent example project across many different language drivers here. You will find the implementations for all drivers as individual GitHub repositories, which you can clone and deploy directly.

Learn with GraphAcademy

badge

Building Neo4j Applications with Java

In this free course, we walk through the steps to integrate Neo4j into your Java projects. You will learn about the Neo4j Java Driver, how sessions and transactions work, how to query Neo4j and process results.

Neo4j Java Driver

The Neo4j Java driver is officially supported by Neo4j and connects to the database using the binary protocol. It aims to be minimal, while being idiomatic to Java. We support Java 8 and 11 for the driver.

When using Maven, add this to your pom.xml file:

<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>5.7.0</version>

For Gradle or Grails, this is how to add the dependency:

compile 'org.neo4j.driver:neo4j-java-driver:5.7.0'

For other build systems, see information available at Maven Central.

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Query;

import static org.neo4j.driver.Values.parameters;

public class HelloWorldExample implements AutoCloseable {
    private final Driver driver;

    public HelloWorldExample(String uri, String user, String password) {
        driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
    }

    @Override
    public void close() throws RuntimeException {
        driver.close();
    }

    public void printGreeting(final String message) {
        try (var session = driver.session()) {
            var greeting = session.executeWrite(tx -> {
                var query = new Query("CREATE (a:Greeting) SET a.message = $message RETURN a.message + ', from node ' + id(a)", parameters("message", message));
                var result = tx.run(query);
                return result.single().get(0).asString();
            });
            System.out.println(greeting);
        }
    }

    public static void main(String... args) {
        try (var greeter = new HelloWorldExample("bolt://localhost:7687", "neo4j", "password")) {
            greeter.printGreeting("hello, world");
        }
    }
}

Driver Configuration

From Neo4j version 4.0 and onwards, the default encryption setting is off by default and Neo4j will no longer generate self-signed certificates. This applies to default installations, installations through Neo4j Desktop and Docker images. You can verify the encryption level of your server by checking the dbms.connector.bolt.enabled key in neo4j.conf.

Table 1. Table Scheme Usage
Certificate Type Neo4j Causal Cluster Neo4j Standalone Server Direct Connection to Cluster Member

Unencrypted

neo4j

neo4j

bolt

Encrypted with Full Certificate

neo4j+s

neo4j+s

bolt+s

Encrypted with Self-Signed Certificate

neo4j+ssc

neo4j+ssc

bolt+ssc

Neo4j AuraDB

neo4j+s

N/A

N/A

Please review your SSL Framework settings when going into production. If necessary, you can also generate certificates for Neo4j with Letsencrypt

Name

Version

Authors

neo4j-java-driver

5.7.0

The Neo4j Team

Package

Example

Neo4j Online Community

Docs

API

Source