java connect neo4j

javaneo4j

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 = session.run("MATCH (n) RETURN n LIMIT 1");

            // Print the results
            while (result.hasNext()) {
                Record record = result.next();
                System.out.println(record.get("n").asNode().get("name").asString());
            }
        } finally {
            // Close the driver
            driver.close();
        }
    }
}

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


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

You can find more information about the Neo4j Java Driver in the Neo4j documentation: https://neo4j.com/docs/java-reference/current/