Skip to content

Commit 988e38e

Browse files
committed
Updated overview with new example and added syntax highlighting
1 parent a1f737a commit 988e38e

File tree

3 files changed

+151
-13
lines changed

3 files changed

+151
-13
lines changed

driver/src/main/javadoc/overview.html

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,74 @@
55
</HEAD>
66
<BODY>
77

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>
119

12-
<h3>Getting Connected</h3>
10+
<h3>Example</h3>
1311

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.*;
1513

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
1817
{
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)
2142
{
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();
2473
}
2574
}
2675
</code></pre>
2776

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-
3077
</BODY>
3178
</HTML>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Copyright (c) 2002-2017 "Neo Technology,"
3+
* Network Engine for Objects in Lund AB [http://neotechnology.com]
4+
*
5+
* This file is part of Neo4j.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
20+
package org.neo4j.docs.driver;
21+
22+
// NOTE: This file holds a copy of the example in overview.html
23+
24+
import org.neo4j.driver.v1.*;
25+
26+
import static org.neo4j.driver.v1.Values.parameters;
27+
28+
public class SmallExample
29+
{
30+
// Driver objects are thread-safe and are typically made available application-wide.
31+
Driver driver;
32+
33+
public SmallExample(String uri, String user, String password)
34+
{
35+
driver = GraphDatabase.driver(uri, AuthTokens.basic(user, password));
36+
}
37+
38+
private void addPerson(String name)
39+
{
40+
// Sessions are lightweight and disposable connection wrappers.
41+
try (Session session = driver.session())
42+
{
43+
// Wrapping Cypher in an explicit transaction provides atomicity
44+
// and makes handling errors much easier.
45+
try (Transaction tx = session.beginTransaction())
46+
{
47+
tx.run("MERGE (a:Person {name: {x}})", parameters("x", name));
48+
tx.success(); // Mark this write as successful.
49+
}
50+
}
51+
}
52+
53+
private void printPeople(String initial)
54+
{
55+
try (Session session = driver.session())
56+
{
57+
// Auto-commit transactions are a quick and easy way to wrap a read.
58+
StatementResult result = session.run(
59+
"MATCH (a:Person) WHERE a.name STARTS WITH {x} RETURN a.name AS name",
60+
parameters("x", initial));
61+
// Each Cypher execution returns a stream of records.
62+
while (result.hasNext())
63+
{
64+
Record record = result.next();
65+
// Values can be extracted from a record by index or name.
66+
System.out.println(record.get("name").asString());
67+
}
68+
}
69+
}
70+
71+
public void close()
72+
{
73+
// Closing a driver immediately shuts down all open connections.
74+
driver.close();
75+
}
76+
77+
public static void main(String... args)
78+
{
79+
SmallExample example = new SmallExample("bolt://localhost:7687", "neo4j", "password");
80+
example.addPerson("Ada");
81+
example.addPerson("Alice");
82+
example.addPerson("Bob");
83+
example.printPeople("A");
84+
example.close();
85+
}
86+
}

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,11 @@
130130
<charset>UTF-8</charset>
131131
<docencoding>UTF-8</docencoding>
132132
<encoding>UTF-8</encoding>
133+
<top><![CDATA[
134+
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/styles/default.min.css">
135+
<script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.9.0/highlight.min.js"></script>
136+
<script>hljs.initHighlightingOnLoad();</script>
137+
]]></top>
133138
</configuration>
134139
<executions>
135140
<execution>

0 commit comments

Comments
 (0)