java connect neo4j


To connect to a Neo4j database from a Java application, you will need to use the Neo4j Java Driver. The driver provides a straightforward and simple way to integrate Neo4j into your Java application.

Here’s an example of how you can use the driver to connect to a Neo4j database and execute a simple Cypher query:

import org.neo4j.driver.v1.*;

public class Main {
    public static void main(String[] args) {
        // Replace with your own connection details
        String uri = "bolt://localhost:7687";
        String user = "neo4j";
        String password = "password";

        Driver driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));

        try (Session session = driver.session()) {
            // Run a simple query
            StatementResult result ="MATCH (n) RETURN n LIMIT 1");

            // Print the results
            while (result.hasNext()) {
                Record record =;
        } finally {
            // Close the driver

To use the driver, you will need to add the following dependency to your project:


You can find more information about the Neo4j Java Driver in the Neo4j documentation: