Neo4j java连接无法正常工作

时间:2017-06-12 10:21:40

标签: java neo4j neo4j-java-api

    import org.neo4j.driver.internal.spi.*;
import org.neo4j.driver.v1.*;
import org.neo4j.driver.v1.Driver;
import org.neo4j.jdbc.ResultSet;
import org.neo4j.jdbc.bolt.BoltConnection;
import org.neo4j.jdbc.bolt.BoltDriver;


import static org.neo4j.driver.v1.Config.build;
import static org.neo4j.driver.v1.Values.parameters;



public class Main {
    static final String url1 = "bolt://127.0.0.1:7687";
    static final String url2 = "http://localhost:7474";

    static final String url3 = "bolt://localhost:7474";
    static final String url4 = "bolt://neo4j:neo4j@localhost";

    static final String url5 = "bolt://localhost";

    public static void main(String[] args) {
        System.out.println("Hello World!");

        Config noSSL = build()
                .withEncryptionLevel(Config.EncryptionLevel.NONE).toConfig();

        Config.ConfigBuilder builder = build();
        builder.withEncryption().toConfig();
      Config config=  builder.withoutEncryption().toConfig();

        Driver driver = GraphDatabase.driver( /*Util.getNeo4jUrl()*/ url5,  AuthTokens.basic( "neo4j", "neo4j" ),config );


        try (Session session = driver.session()){
            if(session.isOpen() == false){
                System.out.println("sesion is closed");
                return;
            }
            session.run( "CREATE (a:Person {name: {name}, title: {title}})",
                    parameters( "name", "Arthur", "title", "King" ) );

            StatementResult result = session.run( "MATCH (a:Person) WHERE a.name = {name} " +
                            "RETURN a.name AS name, a.title AS title",
                    parameters( "name", "Arthur" ) );
            while ( result.hasNext() )
            {
                Record record = result.next();
                System.out.println( record.get( "title" ).asString() + " " + record.get( "name" ).asString() );
            }

            session.close();
            driver.close();
        }


    }
}

这是我的代码c& p来自起始页面中的示例。我尝试了所有网址和2个配置(“noSSL”已弃用)

当我尝试使用url5,url4和url1时,我总是收到此消息。

线程“main”中的异常org.neo4j.driver.v1.exceptions.AuthenticationException:由于身份验证失败,客户端未经授权。

我总是在浏览器“http://localhost:7474/browser/

中使用这些凭据登录

2 个答案:

答案 0 :(得分:0)

尝试更改这些行:

Config noSSL = build().withEncryptionLevel(Config.EncryptionLevel.NONE).toConfig();

Config.ConfigBuilder builder = build();
builder.withEncryption().toConfig();
Config config=  builder.withoutEncryption().toConfig();

Driver driver = GraphDatabase.driver( /*Util.getNeo4jUrl()*/ url5, AuthTokens.basic( "neo4j", "neo4j" ),config );

通过这些:

Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "neo4j" ) );
Session session = driver.session();

答案 1 :(得分:0)

设置新密码:

删除文件:

neo4j-community-3.5.2\data\dbms\auth

在neo4j-community-3.5.2 \ bin目录中执行:

neo4j-admin set-initial-password NewPassword

即例如:

neo4j-admin set-initial-password neo4j

最初的用户是:neo4j

即然后您可以登录:

AuthTokens.basic("neo4j", "neo4j")