Neo4j Java API,如何指定创建数据库

时间:2016-07-11 15:57:23

标签: java database eclipse neo4j

根据Neo4j的文档,我在下面尝试使用Eclipse中的Neo4j Java API创建数据库:

GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase("D:/Eclipse/Workplace/test");

并尝试在Neo4j文件夹("D:\Neo4j3.0.1\workplace3")下设置路径。但我得到了同样的错误:

The method newEmbeddedDatabase(File) in the type GraphDatabaseFactory is not applicable for the arguments (String)

然后我尝试导入java.io.File;并添加:

File dbpath = new File("D:/Neo4j3.0.1/workplace3");
org.neo4j.graphdb.GraphDatabaseService db = dbFactory.newEmbeddedDatabase(dbpath);

然后没有使用前两个包:org.neo4j.graphdb.GraphDatabaseService;org.neo4j.graphdb.Transaction;但是我可以编译程序并显示"成功完成"在Eclipse控制台中。

当我尝试使用neo4j-ce.exe连接到数据库时,它给了我以下警告:

Starting Neo4j failed: Component org.neo4j.server.database.LifecycleManagingDatabase@397b7f" was successfully initialized, but failed to start. Please see attached cause exception.

我的代码:

package com.peterlan522.neo4j.java.example;

import java.io.File;

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

import org.neo4j.graphdb.Label;
import org.neo4j.graphdb.RelationshipType;

public class Neo4jJavaAPIDBOperation {

public enum Tutorials implements Label { JAVA, SCALA, SQL, NEO4J,}
public enum TutorialRelationships implements RelationshipType { JVM_LANGIAGES, NON_JVM_LANGIAGES,}

public static void main(String[] args) {

    org.neo4j.graphdb.factory.GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
    File dbpath = new File("D:/Neo4j3.0.1/workplace3");

    //GraphDatabaseService db = new GraphDatabaseFactory().newEmbeddedDatabase("D:/Eclipse/Workplace/test");

    org.neo4j.graphdb.GraphDatabaseService db = dbFactory.newEmbeddedDatabase(dbpath);

    try (org.neo4j.graphdb.Transaction tx = db.beginTx()) {

        Node javaNode = db.createNode(Tutorials.JAVA);
        javaNode.setProperty("TutorialID", "JAVA001");
        javaNode.setProperty("Title", "Learn Java");
        javaNode.setProperty("NoOfChapters", "25");
        javaNode.setProperty("Status", "Completed");                

        Node scalaNode = db.createNode(Tutorials.SCALA);
        scalaNode.setProperty("TutorialID", "SCALA001");
        scalaNode.setProperty("Title", "Learn Scala");
        scalaNode.setProperty("NoOfChapters", "20");
        scalaNode.setProperty("Status", "Completed");

        Relationship relationship = javaNode.createRelationshipTo
        (scalaNode,TutorialRelationships.JVM_LANGIAGES);
        relationship.setProperty("Id","1234");
        relationship.setProperty("OOPS","YES");
        relationship.setProperty("FP","YES");
        tx.success();
        }
        System.out.print("Done successfully");
        }
}

有人可以提供帮助吗?并提供可执行的示例。非常感谢你!

以下是软件版本: Neo4j社区版3.0.1, Eclipse Mars(4.5.0), Java 1.8.0_91, JRE系统库:JavaSE-1.8

请在下面的链接中查看log.txt: https://drive.google.com/file/d/0B2xDq3--mwK4a0FoanlDengzVWs/view?usp=sharing

0 个答案:

没有答案