Connecting to Neo4j Database from a Java Program using JDBC

This class demonstrate how you can connect to the Neo4j database using their JDBC driver and create nodes/relationships from a Java Program. We have used neo4j-desktop-2.2.2.jar for compiling and running this program.

package com.icodejava.blog.published.neo4j;

/**
 * @author Kushal Paudyal
 * Created on 11/29/2016
 * Last modified on 11/29/2016
 * 
 * This class demonstrate how you can connect to the Neo4j database using their JDBC driver
 * and create nodes/relationships from a Java Program.
 *
 */
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Label;
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 Neo4jJDBC {

    
    /**
     * To avoid database lock errors, please run this program by shutting down the neo4j server.
     */

    public static void main(String[] args) {

        GraphDatabaseFactory dbFactory = new GraphDatabaseFactory();
        GraphDatabaseService db = dbFactory.newEmbeddedDatabase("C:/Users/adam/Desktop/Neo4jDatabaseJDBC");
        try {
            Transaction tx = db.beginTx();

            /**
             * Create an Employee node with some properties
             */
            Node person = db.createNode(Labels.EMPLOYEE);
            person.setProperty("WorkID", "1001");
            person.setProperty("FirstName", "James");
            person.setProperty("MiddleName", "A");
            person.setProperty("LastName", "Tyson");
            

            /**
             * Create a company
             */
            Node company = db.createNode(Labels.COMPANY);
            company.setProperty("CompanyName", "Sanjaal Corps");
            
            /**
             * Create relationship between the person and copany
             */
            Relationship relationship = person.createRelationshipTo(company,Relationships.WORKS_FOR);
            relationship.setProperty("Id", "r21109");
            relationship.setProperty("Since", "2005");

            tx.success();
            
            System.out.println("Successfully created nodes and relationships in the database");
        } finally {
            
        }

    }
    
    public enum Labels implements Label {
        EMPLOYEE, CONTRACTOR, COMPANY, SCHOOL
    }
    
    
    public enum Relationships implements RelationshipType {
        WORKS_FOR, CONTRACTS_FOR  
    }

}

[My Video Promotion]
Tagged , , , , , . Bookmark the permalink.

2 Responses to Neo4j Cheat Sheet – How to do certain things in Neo4j Graph Database

  1. adam says:

    I tried almost every combination to load my data sample from csv to neo4j but it doesnt work. can you help me ? i am trying to load from my local machine and it is giving understandable errors.

  2. Thet Thet says:

    I can not load local csv file into neo4j
    LOAD CSV WITH HEADERS FROM “file:D:/export.csv” AS line return count(*);
    URI is not hierarchical.
    some time
    LOAD CSV WITH HEADERS FROM “file:///export.csv” AS line return count(*);
    error is
    Couldn’t load the external resource at: file:/C:/Users/User/Documents/Neo4j/default.graphdb/import/export.csv
    please help my error

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.