Using Neo4j from Java

If you are a Java developer, this guide provides an overview of options for connecting to Neo4j. While this guide is not comprehensive, it introduces the different APIs and links to the relevant resources.

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 5.x, use Java 17 and your preferred IDE.

5.x driver series requires Java 17. For more details, see Neo4j Java Driver Manual.

Neo4j for Java developers

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.

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 17 for the 5.x driver series.

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

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

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

compile 'org.neo4j.driver:neo4j-java-driver:5.22.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 and Neo4j no longer generates 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 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

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.22.0

The Neo4j Team

Package

Example

Neo4j Online Community

Docs

API

Source