通过Eclipse将Neo4j与java连接,并进行查询

时间:2016-03-03 09:42:59

标签: java neo4j

我正在努力通过Eclipse连接Neo4j和Java,当我运行它运行的代码时,我得到了节点及它们之间的关系,现在我需要有一个密码查询,我尝试了很多方法,但是它们没有工作?

我看过this,但我遇到了错误。

[EDITED]

这是我的代码:

package testtest;

import org.neo4j.graphdb.Direction;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.RelationshipType;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

public class Testnn {

    // This is the path of the Neo4J

    private static final String Neo4J_DBPath = "/Users/nahla.INTRA/Documents/Neo4j/default.graphdb";


    Node first;
    Node second;
    Relationship relation;
    GraphDatabaseService graphDataService;

    // List of Relationships first knows second 

    private static enum RelTypes implements RelationshipType
    {
        KNOWS
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Testnn hello = new Testnn();
        hello.createDatabase();
        hello.removeData();
        hello.shutDown();

    }

    void createDatabase ()  
    {
        // GraphDatabaseService

        graphDataService = new GraphDatabaseFactory().newEmbeddedDatabase(Neo4J_DBPath);

        //Begin Transaction  
        Transaction transaction = graphDataService.beginTx();

        try{

            // create nodes and set the properties

            first = graphDataService.createNode();
            first.setProperty("name", "Tom");

            second = graphDataService.createNode();
            second.setProperty("name", "Sara");

            // Relationship 

            relation = first.createRelationshipTo(second, RelTypes.KNOWS);
            relation.setProperty("relationship-type", "KNOWS");

            System.out.println(first.getProperty("name").toString());
            System.out.println(relation.getProperty("relationship-type").toString());           
            System.out.println(second.getProperty("name").toString());

            // success Transaction

            transaction.success();

        }

        finally {
            //finish the Transaction
            transaction.finish();
        }

        }

    void removeData ()
    {
        Transaction transaction = graphDataService.beginTx();
        try 
        {

            // delete
            first.getSingleRelationship(RelTypes.KNOWS, Direction.OUTGOING).delete();

            System.out.println("Nodes are removed");

            // delete nodes
            first.delete();
            second.delete();
            transaction.success();
            }

        finally
        {

        // finish the transaction
            transaction.finish();
        }
    }

    void shutDown ()    
    {
        // shut down graphDataService
        graphDataService.shutdown();
        System.out.println("Neo4J database is shutdown");
    }
}

现在我有了查询部分:

try ( Transaction ignored = db.beginTx();
      Result result = db.execute( "match (n {name: 'my node'}) return n, n.name" ) )
{
    while ( result.hasNext() )
    {
        Map<String,Object> row = result.next();
        for ( Entry<String,Object> column : row.entrySet() )
        {
            rows += column.getKey() + ": " + column.getValue() + "; ";
        }
        rows += "\n";
    }
}

所以,,,首先如何将第二部分代码包含到第一部分,可能是我没有把它放在正确的位置,第二件事:错误是{结果无法解决}

0 个答案:

没有答案