Graph Java Example: Create Stunning Visuals with Easy Code

Robert Jul 13, 2026

Graph databases like Neo4j provide a powerful way to store, query, and analyze complex, interconnected data. If you're new to graph programming, Java is an excellent language to start with, given its wide use and extensive libraries. Let's explore a simple yet insightful graph example using the Spring Boot framework with the Neo4j Java driver.

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually

First, add Neo4j driver and Spring Boot dependencies to your `build.gradle` or `pom.xml` file. For Gradle, include `org.neo4j:neo4jdriver` and `org.springframework.boot:spring-boot-starter-web`; for Maven, use `neo4j drivers` artifact and `spring-boot-starter-web`.

Graphic Java Algorithms: Graphic Algorithms Java
Graphic Java Algorithms: Graphic Algorithms Java

Setting Up the Project and Database

The first step is to establish a connection to your Neo4j instance. Use the following code snippet to set up a `Session` object with a `GraphDatabaseService`.

Data structures
Data structures

```java import org.neo4j.driver.Session; import org.neo4j.driver.GraphDatabase; ... Session session = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "password" ) ).session(); ```

Defining Nodes and Relationships

a diagram showing the different types of graphs
a diagram showing the different types of graphs

Now, let's define two node labels, `:Person` and `:Movie`, and a relationship `:ACTED_IN`. We'll create nodes and relationships using the `CREATE` clause.

```cypher CREATE (p1:Person {name: 'Tom Hanks'}) CREATE (p2:Person {name: 'Meg Ryan'}) CREATE (m:Movie { title: 'Sleepless in Seattle'}) CREATE (p1)-[:ACTED_IN]->(m) CREATE (p2)-[:ACTED_IN]->(m) ```

Querying the Graph

12 Best Free Udemy Courses to Learn Data Structure and Algorithms in Java, Python, and C++ in 2025 [UPDATED]
12 Best Free Udemy Courses to Learn Data Structure and Algorithms in Java, Python, and C++ in 2025 [UPDATED]

With the graph structure in place, we can query it using Cypher. Let's find all movies that 'Tom Hanks' acted in.

```java try (Session session = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic("neo4j", "password")).session()) { String query = "MATCH (p:Person {name: 'Tom Hanks'})-[ACTED_IN]->(m:Movie) RETURN m.title"; System.out.println(session.run(query).list()); } ```

Importing Data from CSV

Java TreeMap Infographic for Beginners (2025) - Quipoin
Java TreeMap Infographic for Beginners (2025) - Quipoin

For larger datasets, importing data from CSV files can be more efficient. We'll use the `LOAD CSV` command to import actors and movies data.

```cypher LOAD CSV WITH HEADERS FROM "file:///import/actors.csv" AS csv CREATE (p:Person { name: csv.name, born: toInteger(csv.born), died: toInteger(csv.died) }) ```

What are Arrays in Java?
What are Arrays in Java?
Java Cheatsheet
Java Cheatsheet
Java Thread | Multithreading Basics
Java Thread | Multithreading Basics
graph as adjacency list
graph as adjacency list
a diagram with several different types of words
a diagram with several different types of words
a diagram that shows the different types of web pages and what they are used for
a diagram that shows the different types of web pages and what they are used for
Life Cycle of a java program
Life Cycle of a java program
concept of java #java  #javanotes
concept of java #java #javanotes
Java HashMap Explained πŸ—ΊοΈ | DSA Pattern Cheat Sheet for Coding Interviews
Java HashMap Explained πŸ—ΊοΈ | DSA Pattern Cheat Sheet for Coding Interviews
ap Computer Science java programming notes
ap Computer Science java programming notes
Java RoadMap
Java RoadMap
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide
Master Two Pointers Technique πŸ’» | Java DSA Interview Preparation Guide
The 2024 Java Developer RoadMap
The 2024 Java Developer RoadMap
Algorithms Java: Explains Algorithms with Beautiful Pictures Learn it Easy Better and Well
Algorithms Java: Explains Algorithms with Beautiful Pictures Learn it Easy Better and Well
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
a poster with different types of writing and numbers on it, including the words'jwa beginner notes '
Java Data Types Made Easy for Beginners
Java Data Types Made Easy for Beginners
GENERICS IN JAVA
GENERICS IN JAVA
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
β˜• Java Basics Cheat Sheet for Beginners | Learn Java in One Page πŸš€
Java Data Types Explained
Java Data Types Explained

Working with Relationships

After creating nodes, we can connect them using the `MATCH`, `MERGE`, and `CREATE` clauses.

```cypher MATCH (a:Person {name: 'Tom Hanks'}), (b:Person {name: 'Meg Ryan'}) MERGE (a)-[:ACTED_IN]->(b) ```

With this example, we've explored creating a simple graph using Neo4j's Java driver, querying the graph, and importing data from CSV files. Graph databases offer immense potential for analyzing complex networks, and Java provides the perfect platform to leverage this power.