简体   繁体   中英

Neo4j java connection not working

    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();
        }


    }
}

Here is my code c&p from example in start page. I tried all urls and also 2 configs ("noSSL" is deprecated)

I always get this message when I tried with url5, url4 and url1.

Exception in thread "main" org.neo4j.driver.v1.exceptions.AuthenticationException: The client is unauthorized due to authentication failure.

I always login with these credentials in browser " http://localhost:7474/browser/ "

Try changing these lines:

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 );

by these:

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

set new password:

remove file:

neo4j-community-3.5.2\data\dbms\auth

execute in neo4j-community-3.5.2\\bin directory:

neo4j-admin set-initial-password NewPassword

ie for example:

neo4j-admin set-initial-password neo4j

the initial user is: neo4j

ie then you can login with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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