Neo4j Java Driver 6.2 API

These pages document the official Neo4j driver for Java.

Example

import java.util.Map;
import org.neo4j.driver.*;

final class SmallExample {
    // Driver objects are thread-safe and are typically made available application-wide.
    private final Driver driver;

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

    private void addPerson(String name) {
        driver.executableQuery("MERGE (a:Person {name: $x})")
                .withParameters(Map.of("x", name))
                .execute();
    }

    private void printPeople(String initial) {
        driver.executableQuery("MATCH (a:Person) WHERE a.name STARTS WITH $x RETURN a.name AS name")
                .withParameters(Map.of("x", initial))
                .withConfig(QueryConfig.builder().withRouting(RoutingControl.READ).build())
                .execute()
                .records()
                .forEach(record -> System.out.println(record.get("name").asString()));
    }

    public void close() {
        // Closing a driver immediately shuts down all open connections.
        driver.close();
    }

    public static void main(String... args) {
        var example = new SmallExample("bolt://localhost:7687", "neo4j", "password");
        example.addPerson("Ada");
        example.addPerson("Alice");
        example.addPerson("Bob");
        example.printPeople("A");
        example.close();
    }
}
Modules
Module
Description
The Neo4j Java Driver module.