cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to switch between databases in neo4j using java

So I'm using neo4j-enterprise edition loaded with docker.

version: '3'
services:
  neo4j:
    image: neo4j:4.3.2-enterprise
    hostname: neo4j
    container_name: neo4j
    ports:
      - "7474:7474"
      - "7687:7687"
    environment:
      - NEO4J_ACCEPT_LICENSE_AGREEMENT=yes
      - NEO4J_dbms_memory_heap_maxSize=1G
      - NEO4JLABS_PLUGINS=["apoc"]
      - NEO4J_AUTH=neo4j/test
      - NEO4J_apoc_import_file_enabled=true
      - NEO4J_dbms_security_procedures_whitelist= apoc.*
      - NEO4J_dbms_security_procedures_unrestricted= apoc.*

I'm using bolt driver to connect with the neo4j.

public Neo4jSessionFactory(@ConfigProperty(name = "quarkus.neo4j.uri")String databaseUri,
                               @ConfigProperty(name = "quarkus.neo4j.authentication.username")String username,
                               @ConfigProperty(name = "quarkus.neo4j.authentication.password")String password) {
        Configuration neoConfig = new Configuration.Builder().uri(databaseUri**{bolt://localhost:7687}**).credentials(username, password)
                .useNativeTypes().build();
        sessionFactory = new SessionFactory(neoConfig, PACKAGES);
        LOGGER.info("Connection with Neo4j is successful");
    }

Im able to create a seperate database using "CREATE DATABASE <name>" cmd.

However I want to run my queries across the newly created Database. I can simply call "session.query("cypher_query_to_insert")" the data will get loaded to default database neo4j. We can switch between databases in the browser using :use <database name> . However when the cmd is run inside session.query(":use <database_name>") Im getting the following error:

Data Insertion failed. Cause: org.neo4j.driver.exceptions.ClientException: Invalid input ':': expected (line 1, column 1 (offset: 0)) ":use neo4j"

If anyone knows a solution let me know.

1 REPLY 1

Hi @nirmalmahen ,

The :use is a client-side command that is interpreted directly by Neo4j Browser. When using a driver, you can specify the database as part of creating a session. I'm unsure about the proper API when using the Configuration.Builder() API, but you should find autocomplete for something about withDatabase

-ABK