简体   繁体   English

将 neo4j 数据库与 java api 连接

[英]connect neo4j database with java api

Good morning,早上好,

I get an error executing this query in neo4j: The query cannot conclude with USE GRAPH (must be RETURN or an update clause) (line 1, column 5 (offset: 4))我在 neo4j 中执行此查询时出错:查询无法以 USE GRAPH 结束(必须是 RETURN 或更新子句)(第 1 行,第 5 列(偏移量:4))

"USE eindb;" “使用 eindb;”

package org.einaudi.impfilesjson;
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;

public class App implements AutoCloseable {
    
    private final Driver driver;
    
    public App(String uri, String user, String password) {
        driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ) );
    }
    
    @Override
    public void close() throws Exception
    {
        driver.close();
    }
    
    public void print greeting( final String message )
    {
        try ( Session session = driver.session() )
        {
            Result res = session.run( "USE eindb;" );
            System.out.println(res.toString());
        }
    }
    
    public static void main( String[] args ){
        System.out.println( "Hello World!" );
        
        App app;
        
        try {
            
            app = new App( "bolt://localhost:11003", "neo4j", "Admpa" );
            app.printGreeting( "hello, world" );
        }catch(Exception e) {
            System.out.println(e.getMessage());
        }
        
    }
}

How to choose a specific database in neo4j via java API?如何通过 java API 在 neo4j 中选择特定数据库?

This section of the documentation will help you.文档的这一部分将为您提供帮助。 https://neo4j.com/docs/java-manual/current/cypher-workflow/#java-database-selection https://neo4j.com/docs/java-manual/current/cypher-workflow/#java-database-selection

This would set a database named "foo" as the default for the session.这会将名为“foo”的数据库设置为 session 的默认值。

try ( Session session = driver.session(SessionConfig.forDatabase( "foo" )) )

You could also specify the database in your cypher query, but you need more than just the USE clause.您还可以在 cypher 查询中指定数据库,但您需要的不仅仅是USE子句。

Result res = session.run( "USE eindb match (n) return count(n) as nodeCount" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM