0

I've installed the Memgraph platform using Docker. What do I need to do to connect to Memgrpah from Java? I use Java driver. I can't run the program.

I've tried to use the following code, but it doesn't work for me:

import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import org.neo4j.driver.Result;
import org.neo4j.driver.Transaction;
import org.neo4j.driver.TransactionWork;

import static org.neo4j.driver.Values.parameters;

public class HelloWorld implements AutoCloseable
{
    private final Driver driver;

    public HelloWorld( String uri, String user, String password )
    {
        driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ) );
    }

    public void close() throws Exception
    {
        driver.close();
    }

    public void printGreeting( final String message )
    {
        try ( Session session = driver.session() )
        {
            String greeting = session.writeTransaction( new TransactionWork<String>()
            {
                @Override
                public String execute( Transaction tx )
                {
                    Result result = tx.run( "CREATE (a:Greeting) " +
                                                     "SET a.message = $message " +
                                                     "RETURN 'Node ' + id(a) + ': ' + a.message",
                            parameters( "message", message ) );
                    return result.single().get( 0 ).asString();
                }
            } );
            System.out.println( greeting );
        }
    }

    public static void main( String... args ) throws Exception
    {
        try ( HelloWorld greeter = new HelloWorld( "bolt://localhost:7687", "", "" ) )
        {
            greeter.printGreeting( "Hello, World!" );
        }
    }
}
KWriter
  • 1,024
  • 4
  • 22

1 Answers1

0

According to the official Memgraph documentation the code is correct. You are most probably missing the Bolt Java driver.

Try to include the following dependency:

<dependencies>
    <dependency>
      <groupId>org.memgraph</groupId>
      <artifactId>bolt-java-driver</artifactId>
      <version>0.4.7</version>
    </dependency>
  </dependencies>
KWriter
  • 1,024
  • 4
  • 22