Skip to content

Commit

Permalink
Working example for neo4j connection
Browse files Browse the repository at this point in the history
  • Loading branch information
LakshyAAAgrawal committed Feb 18, 2021
1 parent 7a09a15 commit 5facd64
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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/
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>

<dependencies>
Expand Down
65 changes: 60 additions & 5 deletions src/main/java/com/KRacR/s2c/App.java
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 5facd64

Please sign in to comment.