|
5 | 5 | </HEAD>
|
6 | 6 | <BODY>
|
7 | 7 |
|
8 |
| -This is the reference driver implementation for <a href="http://neo4j.com">Neo4j</a>, it allows you to connect to a |
9 |
| -Neo4j server and interact with it. |
10 |
| -If you are using Java, this is generally the interface you'd use to integrate Neo4j in your application. |
| 8 | +<p>These pages document the official <a href="https://neo4j.com" target="_top">Neo4j</a> driver for Java.</p> |
11 | 9 |
|
12 |
| -<h3>Getting Connected</h3> |
| 10 | +<h3>Example</h3> |
13 | 11 |
|
14 |
| -<p>Once you have the driver and a connector on your classpath, you can establish sessions with Neo4j.</p> |
| 12 | +<pre><code>import org.neo4j.driver.v1.*; |
15 | 13 |
|
16 |
| -<pre><code>Driver driver = GraphDatabase.driver( "bolt://localhost:7687", AuthTokens.basic( "neo4j", "p4ssw0rd" ) ); |
17 |
| -try( Session session = driver.session() ) |
| 14 | +import static org.neo4j.driver.v1.Values.parameters; |
| 15 | + |
| 16 | +public class SmallExample |
18 | 17 | {
|
19 |
| - StatementResult result = session.run( "RETURN 'hello, world!'" ); |
20 |
| - while( result.hasNext() ) |
| 18 | + // Driver objects are thread-safe and are typically made available application-wide. |
| 19 | + Driver driver; |
| 20 | + |
| 21 | + public SmallExample(String uri, String user, String password) |
| 22 | + { |
| 23 | + driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password)); |
| 24 | + } |
| 25 | + |
| 26 | + private void addPerson(String name) |
| 27 | + { |
| 28 | + // Sessions are lightweight and disposable connection wrappers. |
| 29 | + try (Session session = driver.session()) |
| 30 | + { |
| 31 | + // Wrapping Cypher in an explicit transaction provides atomicity |
| 32 | + // and makes handling errors much easier. |
| 33 | + try (Transaction tx = session.beginTransaction()) |
| 34 | + { |
| 35 | + tx.run("MERGE (a:Person {name: {x}})", parameters("x", name)); |
| 36 | + tx.success(); // Mark this write as successful. |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + private void printPeople(String initial) |
21 | 42 | {
|
22 |
| - Record record = result.next(); |
23 |
| - System.out.println( record.get(0).asString() ); |
| 43 | + try (Session session = driver.session()) |
| 44 | + { |
| 45 | + // Auto-commit transactions are a quick and easy way to wrap a read. |
| 46 | + StatementResult result = session.run( |
| 47 | + "MATCH (a:Person) WHERE a.name STARTS WITH {x} RETURN a.name AS name", |
| 48 | + parameters("x", initial)); |
| 49 | + // Each Cypher execution returns a stream of records. |
| 50 | + while (result.hasNext()) |
| 51 | + { |
| 52 | + Record record = result.next(); |
| 53 | + // Values can be extracted from a record by index or name. |
| 54 | + System.out.println(record.get("name").asString()); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public void close() |
| 60 | + { |
| 61 | + // Closing a driver immediately shuts down all open connections. |
| 62 | + driver.close(); |
| 63 | + } |
| 64 | + |
| 65 | + public static void main(String... args) |
| 66 | + { |
| 67 | + SmallExample example = new SmallExample("bolt://localhost:7687", "neo4j", "password"); |
| 68 | + example.addPerson("Ada"); |
| 69 | + example.addPerson("Alice"); |
| 70 | + example.addPerson("Bob"); |
| 71 | + example.printPeople("A"); |
| 72 | + example.close(); |
24 | 73 | }
|
25 | 74 | }
|
26 | 75 | </code></pre>
|
27 | 76 |
|
28 |
| -Have a look at the <a href="org/neo4j/driver/v1/Driver.html" title="interface in org.neo4j"><code>Driver</code></a>class to get started. |
29 |
| - |
30 | 77 | </BODY>
|
31 | 78 | </HTML>
|
0 commit comments