From 5facd6426dce017d7d430f7a11e1e37e9514627a Mon Sep 17 00:00:00 2001 From: Lakshya A Agrawal Date: Thu, 18 Feb 2021 12:05:53 +0530 Subject: [PATCH] Working example for neo4j connection --- .gitignore | 2 + pom.xml | 4 +- src/main/java/com/KRacR/s2c/App.java | 65 +++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/.gitignore b/.gitignore index 2029f61..37b5286 100644 --- a/.gitignore +++ b/.gitignore @@ -22,3 +22,5 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* target/ +.project +data/ diff --git a/pom.xml b/pom.xml index 8bea1be..27e9e5c 100644 --- a/pom.xml +++ b/pom.xml @@ -14,8 +14,8 @@ UTF-8 - 1.7 - 1.7 + 1.8 + 1.8 diff --git a/src/main/java/com/KRacR/s2c/App.java b/src/main/java/com/KRacR/s2c/App.java index b747c13..9b75bbe 100644 --- a/src/main/java/com/KRacR/s2c/App.java +++ b/src/main/java/com/KRacR/s2c/App.java @@ -1,13 +1,68 @@ package com.KRacR.s2c; -/** - * Hello world! - * - */ +import org.neo4j.driver.*; + +import static org.neo4j.driver.Values.parameters; + +class SmallExample +{ + // Driver objects are thread-safe and are typically made available application-wide. + Driver driver; + + public SmallExample(String uri, String user, String password) + { + driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password)); + } + + public void addPerson(String name) + { + // Sessions are lightweight and disposable connection wrappers. + try (Session session = driver.session()) + { + // Wrapping a Cypher Query in a Managed Transaction provides atomicity + // and makes handling errors much easier. + // Use `session.writeTransaction` for writes and `session.readTransaction` for reading data. + // These methods are also able to handle connection problems and transient errors using an automatic retry mechanism. + session.writeTransaction(tx -> tx.run("MERGE (a:Person {name: $x})", parameters("x", name))); + } + } + + public void printPeople(String initial) + { + try (Session session = driver.session()) + { + // A Managed transaction is a quick and easy way to wrap a Cypher Query. + // The `session.run` method will run the specified Query. + // This simpler method does not use any automatic retry mechanism. + Result result = session.run( + "MATCH (a:Person) WHERE a.name STARTS WITH $x RETURN a.name AS name", + parameters("x", initial)); + // Each Cypher execution returns a stream of records. + while (result.hasNext()) + { + Record record = result.next(); + // Values can be extracted from a record by index or name. + System.out.println(record.get("name").asString()); + } + } + } + + public void close() + { + // Closing a driver immediately shuts down all open connections. + driver.close(); + } +} + public class App { public static void main( String[] args ) { - System.out.println( "Hello World!" ); + SmallExample example = new SmallExample("bolt://localhost:7687", "neo4j", "neo4jnew"); + example.addPerson("Ada"); + example.addPerson("Alice"); + example.addPerson("Bob"); + example.printPeople("A"); + example.close(); } }