28
28
import java .util .Map ;
29
29
import java .util .logging .Level ;
30
30
import java .util .logging .Logger ;
31
+
32
+ import static org .neo4j .driver .Config .TrustStrategy .trustAllCertificates ;
31
33
// end::driver-introduction-example-import[]
32
34
33
35
// tag::driver-introduction-example[]
34
36
public class DriverIntroductionExample implements AutoCloseable {
35
37
private static final Logger LOGGER = Logger .getLogger (DriverIntroductionExample .class .getName ());
36
38
private final Driver driver ;
37
39
38
- public DriverIntroductionExample (String uri , String user , String password ) {
39
- // Aura queries use an encrypted connection
40
- Config config = Config .builder ()
41
- .withEncryption ()
42
- .build ();
43
-
40
+ public DriverIntroductionExample (String uri , String user , String password , Config config ) {
44
41
// The driver is a long living object and should be opened during the start of your application
45
42
driver = GraphDatabase .driver (uri , AuthTokens .basic (user , password ), config );
46
43
}
@@ -64,6 +61,7 @@ public void createFriendship(final String person1Name, final String person2Name)
64
61
params .put ("person2_name" , person2Name );
65
62
66
63
try (Session session = driver .session ()) {
64
+ // Write transactions allow the driver to handle retries and transient errors
67
65
Record record = session .writeTransaction (tx -> {
68
66
Result result = tx .run (createFriendshipQuery , params );
69
67
return result .single ();
@@ -90,7 +88,7 @@ public void findPerson(final String personName) {
90
88
Result result = tx .run (readPersonByNameQuery , params );
91
89
return result .single ();
92
90
});
93
- System .out .println (String .format ("Found person:: %s" , record .get ("name" ).asString ()));
91
+ System .out .println (String .format ("Found person: %s" , record .get ("name" ).asString ()));
94
92
// You should capture any errors along with the query and data for traceability
95
93
} catch (Neo4jException ex ) {
96
94
LOGGER .log (Level .SEVERE , readPersonByNameQuery + " raised an exception" , ex );
@@ -99,10 +97,16 @@ public void findPerson(final String personName) {
99
97
}
100
98
101
99
public static void main (String ... args ) throws Exception {
100
+ // Aura uses the "neo4j" protocol
102
101
String boltUrl = "%%BOLT_URL_PLACEHOLDER%%" ;
102
+
103
103
String user = "<Username for Neo4j Aura database>" ;
104
104
String password = "<Password for Neo4j Aura database>" ;
105
- try (DriverIntroductionExample app = new DriverIntroductionExample (boltUrl , user , password )) {
105
+
106
+ // Aura queries use an encrypted connection
107
+ Config config = Config .builder ().withEncryption ().build ();
108
+
109
+ try (DriverIntroductionExample app = new DriverIntroductionExample (boltUrl , user , password , config )) {
106
110
app .createFriendship ("Alice" , "David" );
107
111
app .findPerson ("Alice" );
108
112
}
0 commit comments