2017-06-12 92 views
1
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:[email protected]"; 

    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從起始頁中的示例。我試過所有的URL和2配置(「noSSL」已棄用)Neo4j java連接不工作

我總是得到這個消息,當我嘗試與url5,url4和url1。

線程「main」中的異常org.neo4j.driver.v1.exceptions.AuthenticationException:由於身份驗證失敗,客戶端未經授權。

我總是在瀏覽器這些憑證 「http://localhost:7474/browser/

+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(); 
+0

遺憾的是沒」同樣的錯誤:/ 當session.beginTransaction();或s ession.run(「CREATE(a:Person {name:{name},title:{title}})」, 參數(「name」,「Arthur」,「title」,「King」)); –

+0

如果您將URL更改爲'http:// localhost:7474'? –

+0

http方案不支持 如果bolt:// localhost:7474 線程「main」中的異常org.neo4j.driver.v1.exceptions.ClientException:服務器響應了HTTP。確保您沒有嘗試連接到http端點(HTTP默認爲端口7474,而BOLT默認爲端口7687) –