3

你能帮我么?我是neo4j的新手。

当我尝试在 Eclipse 控制台中使用 JDBC 动态创建节点时,它显示已创建的节点,但是当我访问 neo4j url 时,它没有显示任何节点。

按照代码

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

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

import Utilities.IConstants;

public class NodesCreationUsingJDBC {

    static GraphDatabaseService db;
    private static final String DB_PATH = IConstants.DB_PATH;

    private static enum RelTypes implements RelationshipType {
        KNOWS
    }

    public static void main(String[] args) throws SQLException, ClassNotFoundException {  
        NodesCreationUsingJDBC nodesCreationUsingJDBC = new NodesCreationUsingJDBC();
        Connection connect = null;
        Statement statement = null;
        ResultSet resultSet = null;
        Class.forName("com.mysql.jdbc.Driver");
        connect = DriverManager.getConnection("jdbc:mysql://localhost/prasad?user=root&password=root");
        statement = connect.createStatement();
        resultSet = statement.executeQuery("select * from user");
        nodesCreationUsingJDBC.createNodes(resultSet);       
    }

    public void createNodes(ResultSet resultSet) throws SQLException {
        db = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder( DB_PATH ).
                setConfig( GraphDatabaseSettings.node_keys_indexable, "firstName,lastName" ).
                setConfig( GraphDatabaseSettings.node_auto_indexing, "true" ).
                setConfig( GraphDatabaseSettings.relationship_auto_indexing, "true" ).newGraphDatabase();
        Transaction tx = db.beginTx();
        String firstName="";
        String lastName="";
        try {
            while (resultSet.next()) {  
                firstName=resultSet.getString("firstname"); 
                lastName=resultSet.getString("lastname"); 
                Node datanode = db.createNode();
                datanode.setProperty("lastName",lastName);
                Node node = db.createNode();
                node.setProperty( firstName, firstName );
                node.createRelationshipTo( datanode, RelTypes.KNOWS );
                System.out.println(datanode.getProperty("lastName"));
                System.out.println("name--------->"+firstName); 
                // createAndConnectNode( firstName, datanode,RelTypes.KNOWS );                     
            }
            tx.success();
        } finally{
            tx.finish();
        }
        System.out.println("BEFORE DB SHUTDOWN");
        db.shutdown();
    }

    public  Node createAndConnectNode( String name, Node otherNode, RelationshipType relatiohshipType ) {
        Node node = db.createNode();
        node.setProperty( name, name );
        node.createRelationshipTo( otherNode, relatiohshipType );
        System.out.println(otherNode);
        System.out.println("name--------->"+name); 
        return node;
    }
}
4

1 回答 1

1

您的问题很可能是从两个不同的位置保存和读取数据。确保您保存节点的 DB_PATH 确实是您从中读取的位置。我建议您安装 Neoclipse 并使用它查看 DB_PATH 的确切内容。

于 2013-12-16T02:24:41.160 回答