About the Driver

In the Cypher Fundamentals course, we cover how to query Neo4j using a language called Cypher. To execute a Cypher statement against a Neo4j database you will use an object called a Driver.

The Driver object is a thread-safe, application-wide fixture from which all Neo4j interaction derives.

The Driver API is topology independent, so you can run the same code against a Neo4j cluster or a single DBMS.

To connect to and query Neo4j from within a Java application, you use the Neo4j Java Driver.

The Neo4j Java Driver is one of five officially supported drivers, the others are JavaScript, .NET, Python, and Go. There are also a wide range of Community Drivers available for other languages including PHP and Ruby.

You should create a single instance of the Driver in your application per Neo4j cluster or DBMS, which can then be shared across your application.

Installing the Driver

The Neo4j Java Driver is available from Maven Central, so you can use it with Maven, Gradle and other build tools.

In our project we use Maven, so you can add the driver with the following dependency to the <dependencies> section of your pom.xml file:

xml
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/pom.xml[tag=driver]

Creating a Driver Instance

Each driver instance will connect to one DBMS, or Neo4j cluster, depending on the value provided in the connection string.

After importing the org.neo4j.driver.* package, you can instantiate a Driver instance from the GraphDatabase.driver() factory method call. The driver() method requires two arguments:

  1. A connection string for the Neo4j cluster or DBMS - for example neo4j://localhost:7687 or neo4j+s://dbhash.databases.neo4j.io:7687

  2. An authentication token - Neo4j supports basic username and password authentication, kerberos tokens or custom authentication. You can create an authentication token by calling one of the static methods provided by AuthTokens.

Here is an example for how to create a driver instance:

java
Creating a Driver Instance
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/main/java/example/Index.java[tag=import]
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/main/java/example/Index.java[tag=credentials,indent=0]
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/main/java/example/Index.java[tag=driver,indent=0]

The above example creates an unencrypted connection to the Neo4j server at localhost on the default port number of 7687. The driver then attemps to authenticate against the server using a basic authentication with the username neo4j and password letmein!.

Verifying Connectivity

You can verify that the connection details used during driver instantiation are correct by calling the verifyConnectivity() function. This function returns the Driver instance if the connection details are correct, or fails with a Neo.ClientError.Security.Unauthorized Exception if a connection could not be made.

java
Verify Connectivity
Unresolved directive in lesson.adoc - include::https://raw.githubusercontent.com/neo4j-graphacademy/llm-vectors-unstructured/main/src/main/java/example/Index.java[tag=verifyConnectivity,indent=0]

Check Your Understanding

1. Which of the following programming languages have officially supported drivers?

  • ✓ .NET

  • ✓ Go

  • ✓ Java

  • ✓ JavaScript

  • ✓ Python

  • ❏ PHP

  • ❏ Ruby

Hint

Five languages are officially supported by Neo4j.

Solution

The five supported languages are .NET, Go, Java, JavaScript and Python.

2. What name is the Neo4j Java Driver registered under on Maven?

  • @neo4j/driver

  • neo4j

  • neo4j-driver

  • org.neo4j.driver:neo4j-java-driver

Hint

The package is registered on maven as org.neo4j.driver:neo4j-java-driver.

Solution

The package is registered on maven as org.neo4j.driver:neo4j-java-driver.

Lesson Summary

In this lesson, you learned about the Neo4j Java Driver and how it can be used to connect to Neo4j from within a Java application.

In the next lesson, we will take a closer look at the first argument in the GraphDatabase.driver() method, the connection string.

Chatbot

Hi, I am an Educational Learning Assistant for Intelligent Network Exploration. You can call me E.L.A.I.N.E.

How can I help you today?