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  
    }

}

Basic example of implementing Singly Linked List in Java

The following is a very basic example of a Singly Linked List implementationin Java. In a nutshell, it’s an object that holds a value for itself and also has a reference to another object in the list, which could be null of the object itself is the tail (i.e. last element in the linked list)

package com.icodejava.blog.published.datastructure;
/**
 * @author Kushal Paudyal
 * Created on - 11/28/2016
 * Last modified on - 11/28/2016
 * This class shows a basic example of defining a SinglyLinkedList in Java
 * Also provides a overridden constructor both of which are optional.
 */
public class SinglyLinkedList {
	
	private Object value;
	private SinglyLinkedList nextObject;
	
	public SinglyLinkedList(Object value) {
		this.value = value;
		this.nextObject = null;
	}
	
	public SinglyLinkedList(Object value, SinglyLinkedList nextObject) {
		this.value = value;
		this.nextObject = nextObject;
	}
	
	
	public Object getValue() {
		return value;
	}
	public void setValue(Object value) {
		this.value = value;
	}
	public SinglyLinkedList getNextObject() {
		return nextObject;
	}
	public void setNextObject(SinglyLinkedList nextObject) {
		this.nextObject = nextObject;
	}

}


Linked List is a data structure that possess following properties:

  • The are a group of nodes linked together by some reference. Singly linked list has reference to next node, while doubly linked list has reference to previous and next nodes.
  • Elements can be inserted indefinitely.
  • They don’t take contiguous memory locations. For this reason, they are suitable to be used even if the disk or memory is fragmented.
  • They allow sequential access to elements while arrays allow random access
  • Extra space is needed for reference. It is impractical to use for boolean or char for space wastage.

Singly linked list might not be a right data structure for all situations, so a care must be taken in choosing the data structure for your problem. As the link in the singly linked list only points to the location of the next object (and not in reverser order), singly linked list can only be traversed in forward direction. Also, you need to know the location of the first node or object to be able to traverser rest of the nodes or objects.