-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working example for neo4j connection
- Loading branch information
1 parent
7a09a15
commit 5facd64
Showing
3 changed files
with
64 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |