Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To connect Android Studio to Neo4j, you can follow the below steps:

  1. Add the Neo4j Bolt driver dependency to your project's build.gradle file:
dependencies {
    compile group:'org.neo4j.driver', name:'neo4j-java-driver', version:'4.0.0'
}
  1. Create a new Java class to handle the Neo4j connection. For example:
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;

public class Neo4jConnection {
    private final Driver driver;
    private final String uri;
    private final String username;
    private final String password;

    public Neo4jConnection(String uri, String username, String password) {
        this.uri = uri;
        this.username = username;
        this.password = password;
        this.driver = GraphDatabase.driver(uri, AuthTokens.basic(username, password));
    }

    public Driver getDriver() {
        return this.driver;
    }
}
  1. Create a new instance of the Neo4jConnection class in your main activity:
private Neo4jConnection neo4jConnection;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    neo4jConnection = new Neo4jConnection("bolt://localhost:7687", "username", "password");
}
  1. Now you can use the Neo4j driver to execute queries and transactions. For example:
try (Session session = neo4jConnection.getDriver().session()) {
    session.writeTransaction(tx -> tx.run("CREATE (n:Person {name: $name})", parameters("name", "John")));
}

This code will create a new node in the Neo4j graph database with the label "Person" and the value "John" for the "name" property.