Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue while creating nodes by using jdbc in neo4j

Tags:

java

neo4j

Can you please help me? I am new to neo4j.

When i am trying to create nodes dynamically by using JDBC in eclipse console it is showing nodes created, but when i access the neo4j url it is not showing any nodes.

Following the code

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;
    }
}
like image 892
user1234 Avatar asked Mar 22 '26 01:03

user1234


1 Answers

Your problem is most likely that the data is being saved and read from two different locations. Ensure that DB_PATH where you saved your nodes is indeed where your reading if back from. I would recommend you install Neoclipse and using it see the exact content of DB_PATH.

like image 193
Mario Dennis Avatar answered Mar 23 '26 13:03

Mario Dennis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!