Java driver

This section presents the breaking changes between the Neo4j 1.7 Java driver and 4.x Java driver.

The latest version of the Java driver for Neo4j can be found on the Java driver’s official page.

Example 1. Example of changes between the 1.7 Java driver and the 4.0 Java driver
Example code for the 4.0 Java driver Example code for the 1.7 Java driver
import org.neo4j.driver.Bookmark;
import org.neo4j.driver.Driver;
import org.neo4j.driver.Query;
import org.neo4j.driver.Result;
import org.neo4j.driver.Session;
import org.neo4j.driver.SessionConfig;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.Values;
...

private final Driver driver;
...
public void printGreeting( String message, Bookmark bookmark )
{
   SessionConfig sessionConfig = SessionConfig.builder()
     .withDatabase( "neo4j" )
     .withDefaultAccessMode( AccessMode.WRITE )
     .withBookmarks( bookmark ).build();

   try ( Session session = driver.session( sessionConfig );
         Transaction transaction = session.beginTransaction() )
   {
      Query query = new Query( "CREATE (a:Greeting) SET a.message = $message RETURN a.message + ', from node ' + id(a)", Values.parameters( "message", message ) );

      Result result = transaction.run( query );
      String greeting = result.single().get( 0 ).asString();
      System.out.println( greeting );
      transaction.commit(); // commit immediately here
   }
}
import org.neo4j.driver.v1.AccessMode;
import org.neo4j.driver.v1.Driver;
import org.neo4j.driver.v1.Session;
import org.neo4j.driver.v1.Statement;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.driver.v1.Transaction;
import org.neo4j.driver.v1.Values;

...

private final Driver driver;
...
public void printGreeting( String message, String bookmark )
{




   try ( Session session = driver.session( AccessMode.WRITE, bookmark );
         Transaction transaction = session.beginTransaction() )
   {
       Statement query = new Statement( "CREATE (a:Greeting) SET a.message = $message RETURN a.message + ', from node ' + id(a)", Values.parameters( "message", message ) );


       StatementResult result = transaction.run( query );
       transaction.success(); // mark success, actually commit will happen in transaction.close()
       String greeting = result.single().get( 0 ).asString();
       System.out.println( greeting );
   }
}