简体   繁体   English

如何使用neo4j-jdbc连接到Neo4j 3.0数据库?

[英]How to connect to Neo4j 3.0 database with neo4j-jdbc?

Hi, 嗨,

I've created a maven project in eclipse and added the dependency: 我在eclipse中创建了一个maven项目并添加了依赖项:

<dependency>
    <groupId>org.neo4j</groupId>
    <artifactId>neo4j-jdbc</artifactId>
    <version>3.0</version>
</dependency>

I'm just trying to get a connection to my db working so that I can move on to integrate the connection with my main project but I'm having trouble getting things off the ground. 我只是想使数据库连接正常,以便我可以继续将连接与主项目集成在一起,但是我很难将其付诸实践。

I've used the code example given on the official repo: 我已经使用了官方仓库中给出的代码示例:

import org.neo4j.jdbc.Connection;
import org.neo4j.jdbc.PreparedStatement;
import org.neo4j.jdbc.ResultSet;

public class Neo4jConnectionTest {

    public static void main(String[] args) {
        // Connect
        Connection con = DriverManager.getConnection(
                                        "jdbc:neo4j:bolt://localhost");

        // Querying
        String query = "MATCH (u:User)-[:FRIEND]-(f:User) 
                        WHERE u.name = {1} 
                        RETURN f.name, f.age";
        try {
            PreparedStatement stmt = con.prepareStatement(query);
            stmt.setString(1,"John");
            ResultSet rs = con.execute();

            while (rs.next()) {
                System.out.println(
                    "Friend: "+rs.getString("f.name")+" is "+rs.getInt("f.age"));
            }
        } catch (Exception e) { e.printStackTrace(); }
        con.close();
    }
}

I am unable to compile this as: 我无法将其编译为:

DriverManager cannot be resolved within the neo4j-jdbc-3.0, 在neo4j-jdbc-3.0中无法解析DriverManager

Prepared stmt = con.prepareStatement(query); causes a type mismatch, 导致类型不匹配,

and con.execute() is undefined for type org.neo4j.jdbc.Connection con.execute()类型的org.neo4j.jdbc.Connection con.execute()未定义

I'd greatly appreciate any advice and expertise on the matter, thanks. 非常感谢您对此事的任何建议和专业知识,谢谢。

By changing to: 通过更改为:

<dependency>
    <groupId>org.neo4j.driver</groupId>
    <artifactId>neo4j-java-driver</artifactId>
    <version>1.0.4</version>
</dependency>

I was able to successfully connect to the Neo4j 3.0 server using an example from Neo4j's site: 我能够使用Neo4j网站上的示例成功连接到Neo4j 3.0服务器:

public static void main(String[] args) {
    Driver driver = GraphDatabase.driver( 
        "bolt://localhost", AuthTokens.basic( "neo4j", "neo4j" ) );
    Session session = driver.session();

    session.run( "CREATE (a:Person {name:'Arthur', title:'King'})" );

    StatementResult result = session.run( 
                                        "MATCH (a:Person) 
                                         WHERE a.name = 'Arthur' 
                                         RETURN a.name AS name, a.title AS title");

    while ( result.hasNext() ) {
        Record record = result.next();
        System.out.println( record.get( "title" ).asString() + 
                                " " + record.get("name").asString() );
    }

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

I thought I'd share as this worked instantly with no hassle. 我以为我会分享,因为这可以立即进行而没有麻烦。

Neo4j Language Guides Neo4j语言指南

DriverManager , Connection , PreparedStatement and ResultSet are all classes or interfaces from the java.sql package, which is part of the JDK. DriverManagerConnectionPreparedStatementResultSet都是来自JDK的java.sql包的所有类或接口。 I guess the documentation assumes you'll use an IDE which will find the correct import for you. 我猜文档假设您将使用一个IDE,它将为您找到正确的导入。

It's the point of using the JDBC driver: you use the JDBC API, not a proprietary one (ie in a vendor package). 这是使用JDBC驱动程序的重点:您使用的是JDBC API,而不是专有的(即在供应商软件包中)。


Update 更新

There was a typo in the neo4j-jdbc readme, it should have read neo4j-jdbc自述文件中有一个错字,应该已经阅读

ResultSet rs = stmt.execute();

instead of 代替

ResultSet rs = con.execute();

otherwise the PreparedStatement has not use (and the code doesn't compile because there's no no-arg overload of Connection.execute() ). 否则, PreparedStatement不会使用(并且因为Connection.execute()没有无参数的重载,所以代码也不会编译)。


Update 2 更新2

The documented dependency was also wrong, as neo4j-jdbc does not contain any driver. 记录的依赖关系也有误,因为neo4j-jdbc不包含任何驱动程序。

You should depend on: 您应该依靠:

  • either the all-in-one module, which gives you the opportunity to use the Bolt or HTTP protocols: 多合一模块,它使您有机会使用Bolt或HTTP协议:

     <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-jdbc-driver</artifactId> <version>3.0</version> </dependency> 
  • or the module for a specific protocol: 或特定协议的模块:

     <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-jdbc-bolt</artifactId> <version>3.0</version> </dependency> <!-- or --> <dependency> <groupId>org.neo4j</groupId> <artifactId>neo4j-jdbc-http</artifactId> <version>3.0</version> </dependency> 

Update 3 更新3

Both problems have now been fixed in the documentation of the project. 这两个问题现在都已在项目文档中修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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