2015-08-27 3 views
3

Я использую neo4j как базу данных графа для моей диссертации, я сталкиваюсь с трудным временем подключения neo4j2.3.1 с простым подключением jdbc.Строка подключения Neo4j

Вот очень простой код, который я использую для подключения к neo4j.

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.ResultSet; 
import java.sql.Statement; 

public class Main { 
    Main(){ 
     try { 
      Class.forName("org.neo4j.jdbc.Driver"); 
     }catch (Exception ex){ 

     } 
    } 

    public static void main(String[] args) { 

     try { 
     Connection con = DriverManager.getConnection("jdbc:neo4j://localhost:7474"); 
     try(Statement stmt = con.createStatement()) { 
      ResultSet rs = stmt.executeQuery("MATCH (n:User) RETURN n.name"); 
      while(rs.next()) { 
       System.out.println(rs.getString("n.name")); 
      } 
     } 

     }catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
    } 
} 

Вот журнал, который показывает, что что-то не так с схемой аутентификации отдыха.

> Aug 27, 2015 10:20:25 PM org.neo4j.jdbc.Driver createDatabases INFO: 
> Embedded Neo4j support not enabled 
> org/neo4j/graphdb/GraphDatabaseService Aug 27, 2015 10:20:25 PM 
> org.neo4j.jdbc.Driver createDatabases INFO: Embedded Neo4j support not 
> enabled org/neo4j/graphdb/GraphDatabaseService Starting the Apache 
> HTTP client Couldn't find any helper support the HTTP_None challenge 
> scheme. Unauthorized (401) - Unauthorized  at 
> org.restlet.resource.ClientResource.doError(ClientResource.java:612) 
> at 
> org.restlet.resource.ClientResource.handleInbound(ClientResource.java:1202) 
> at 
> org.restlet.resource.ClientResource.handle(ClientResource.java:1069) 
> at 
> org.restlet.resource.ClientResource.handle(ClientResource.java:1044) 
> at 
> org.restlet.resource.ClientResource.handle(ClientResource.java:950) 
> at org.restlet.resource.ClientResource.get(ClientResource.java:658) 
> at org.neo4j.jdbc.rest.Resources.readJsonFrom(Resources.java:97) at 
> org.neo4j.jdbc.rest.Resources$DiscoveryClientResource.readInformation(Resources.java:135) 
> at 
> org.neo4j.jdbc.rest.Resources.getDiscoveryResource(Resources.java:65) 
> at 
> org.neo4j.jdbc.rest.Resources.getDiscoveryResource(Resources.java:60) 
> at 
> org.neo4j.jdbc.Neo4jConnection.getDiscoveryResource(Neo4jConnection.java:80) 
> at 
> org.neo4j.jdbc.Neo4jConnection.createExecutor(Neo4jConnection.java:69) 
> at org.neo4j.jdbc.Neo4jConnection.<init>(Neo4jConnection.java:61) at 
> org.neo4j.jdbc.Connections$4.doCreate(Connections.java:51) at 
> org.neo4j.jdbc.Connections.create(Connections.java:62) at 
> org.neo4j.jdbc.Driver.connect(Driver.java:64)  at 
> org.neo4j.jdbc.Driver.connect(Driver.java:36)  at 
> java.sql.DriverManager.getConnection(DriverManager.java:571) 
> Unauthorized (401) - Unauthorized at 
> java.sql.DriverManager.getConnection(DriverManager.java:233) at 
> Main.main(Main.java:20) at 
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
> at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
> at java.lang.reflect.Method.invoke(Method.java:606)  at 
> com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) 

Я использую neo4j2.3.1, поскольку он поставляется с аутентификацией по умолчанию имени пользователя neo4j.

ответ

4

Вы должны импортировать эти Neo4j пакеты,

импорт org.neo4j.jdbc.Driver;

import org.neo4j.jdbc.Neo4jConnection;

попробуйте эти один,

Neo4jConnection conn; 
 
ResultSet rs;  
 
    \t \t 
 
public static void main(String[] args) { 
 
      try{ 
 
       final Driver driver = new Driver(); //org.neo4j.jdbc.Driver \t \t \t 
 
       final Properties props = new Properties(); 
 
       props.put("user", "your username"); 
 
       props.put("password", "your password"); 
 
      \t String url="jdbc:neo4j://localhost:7474"; 
 
       conn = driver.connect(url, props); 
 
      \t 
 
      \t Statement stmt = conn.createStatement() 
 
      \t rs = stmt.executeQuery("MATCH (n:User) RETURN n.name"); 
 
      \t \t while(rs.next()) { 
 
        System.out.println(rs.getString("n.name")); 
 
      \t \t }   \t \t \t \t 
 
       \t } 
 
       catch (SQLException e) { 
 
       throw new RuntimeException(e); 
 
       } 
 
      \t }

+0

Спасибо друг :) –

+0

приветствовать FRD. , , – Karthikeyan

3

Вам необходимо указать пользователя и пароль как свойства соединения JDBC или строки подключения.

Properties properties = new Properties(); 
properties.put("user", "neo4j"); 
properties.put("password", "neo4j"); 

serverConnection = DriverManager.getConnection("jdbc:neo4j://localhost:7474", properties); 
Смежные вопросы