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 will introduce the different APIs and link 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, please use Java 8 or 11 and your favorite IDE.
Intermediate
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 8 and 11 for the driver.
When using Maven, add this to your pom.xml file:
<groupId>org.neo4j.driver</groupId>
<artifactId>neo4j-java-driver</artifactId>
<version>4.4.5</version>
For Gradle or Grails, this is how to add the dependency:
compile 'org.neo4j.driver:neo4j-java-driver:4.4.5'
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.Result;
import org.neo4j.driver.Session;
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 Exception
{
driver.close();
}
public void printGreeting( final String message )
{
try ( Session session = driver.session() )
{
String greeting = session.writeTransaction( tx ->
{
Result result = tx.run( "CREATE (a:Greeting) " +
"SET a.message = $message " +
"RETURN a.message + ', from node ' + id(a)",
parameters( "message", message ) );
return result.single().get( 0 ).asString();
} );
System.out.println( greeting );
}
}
public static void main( String... args ) throws Exception
{
try ( HelloWorldExample 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 by default and Neo4j will no longer generate 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
.
Certificate Type | Neo4j Causal Cluster | Neo4j Standalone Server | Direct Connection to Cluster Member |
---|---|---|---|
Unencrypted |
|
|
|
Encrypted with Full Certificate |
|
|
|
Encrypted with Self-Signed Certificate |
|
|
|
|
N/A |
N/A |
Please 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 |
4.4.5 |
The Neo4j Team |
Was this page helpful?