neo4j-java连接

本例使用neo4j3.0.1架包

使用maven下载架包

        <!-- https://mvnrepository.com/artifact/org.neo4j/neo4j-jdbc-driver -->
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-jdbc-driver</artifactId>
            <version>3.0.1</version>
        </dependency>
                
        <!-- https://mvnrepository.com/artifact/org.neo4j/neo4j -->
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>3.0.1</version>
        </dependency>

在如下设置未开启权限认证时,

# Disable authorization
dbms.security.auth_enabled=false

 

java程序

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

public class TestNeo4jJdbc {
    public static void main(String[] args) throws SQLException {
        Connection con = DriverManager.getConnection("jdbc:neo4j:http://xxxxx:7474", "neo4j", "xxxx");
        // Querying
        try (Statement stmt = con.createStatement()) {
            ResultSet rs = stmt.executeQuery("MATCH (n) RETURN n");
            while (rs.next()) {
                System.out.println(rs.getString("n"));
            }
        }
    }
}

 

 打印

 

如开启权限认真时,发现上面已经无法访问

提示错误

[Neo.ClientError.Security.AuthorizationFailed]:No authorization header suppl
...

 

错误分析请看这里:

http://stackoverflow.com/questions/32505398/neo-clienterror-security-authorizationfailed-for-unmanaged-extension

 

但是看玩还是未解决需要的答案,

 

另一种解决方案,不使用jdbc原生连接,使用neo4j-ogm-http-driver.jar中的org.neo4j.ogm.config.configuration去间接创建sessionFactory和session

这里我使用的是

neo4j-ogm-http-driver-2.0.3.jar

 

具体代码

         Configuration config = new Configuration();
         config.driverConfiguration().setDriverClassName("org.neo4j.ogm.drivers.http.driver.HttpDriver").setURI("http://neo4j:a123456@xxx.xx.xx.xxx:7474");
         SessionFactory sessionFactory = new SessionFactory(config, "xxx.model");
         Session session = sessionFactory.openSession();
         Result result = session.query("MATCH (n) RETURN count(n)",new HashMap<String, String>() );
         Iterable<Map<String, Object>> iterator = result.queryResults();
         for (Map<String, Object> map : result) {
            System.out.println(map);
        }
         

 

打印了结果

 

 

 

初次学校使用neo4j问题真不小

 

posted @ 2016-10-13 15:05  243573295  阅读(2687)  评论(0编辑  收藏  举报