求java连接neo4j的一个简单例子
发布于 7 年前 作者 onepint 6937 次浏览 来自 问答

一些例子老是报错,也不知道是为什么原因。。比如说这个: GraphDatabaseService graphDb = new EmbeddedGraphDatabase( “helloworld” );
Transaction tx = graphDb.beginTx();
try {

    Node firstNode = graphDb.createNode();  
    Node secondNode = graphDb.createNode();  
    firstNode.setProperty( "message", "Hello, " );  
    secondNode.setProperty( "message", "world!" );  
  
    Relationship relationship = firstNode.createRelationshipTo( secondNode,   
        DynamicRelationshipType.of("KNOWS") );  
    relationship.setProperty( "message", "brave Neo4j " );  
    tx.success();  
} finally {  
    tx.finish();  
}  
请问需要怎么更改才能正常运行呢?或者给一个其他的简单代码例子也可以,不胜感激!
5 回复

请问哪里报错了? 我在使用neo4j java api 时也报错了,还没有错误信息 以下方框内出错。 图片.png

neo4j api 3.1.4

确保,驱动版本 和 neo4j 版本是配套的啊

官方有例子: Driver driver = GraphDatabase.driver( “bolt://localhost:7687”, AuthTokens.basic( “neo4j”, “neo4j” ) ); Session session = driver.session();

session.run( “CREATE (wd:Person {name: {name}, title: {title}})”, parameters( “name”, “node”, “title”, “Test” ) );

StatementResult result = session.run( "MATCH (node:Person) WHERE node.name = {name} " + “RETURN node.name AS name, node.title AS title”, parameters( “name”, “node” ) ); while ( result.hasNext() ) { Record record = result.next(); System.out.println( record.get( “title” ).asString() + " " + record.get( “name” ).asString() ); } session.close(); driver.close();

有三种连接方式,GraphDatabaseService这种是嵌入式的,上面的例子用的是driver,还有一种是用jdbc连接,用不同的jar包

回到顶部