|
| 1 | +/* |
| 2 | + * Copyright (c) 2002-2020 "Neo4j," |
| 3 | + * Neo4j Sweden AB [http://neo4j.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 | +package org.neo4j.docs.driver; |
| 20 | + |
| 21 | +// tag::driver-introduction-example-import[] |
| 22 | + |
| 23 | +import org.neo4j.driver.*; |
| 24 | + |
| 25 | +import static org.neo4j.driver.Values.parameters; |
| 26 | +// end::driver-introduction-example-import[] |
| 27 | + |
| 28 | +// tag::driver-introduction-example[] |
| 29 | +public class DriverIntroductionExample implements AutoCloseable |
| 30 | +{ |
| 31 | + private final Driver driver; |
| 32 | + |
| 33 | + public DriverIntroductionExample(String uri, String user, String password ) |
| 34 | + { |
| 35 | + driver = GraphDatabase.driver( uri, AuthTokens.basic( user, password ) ); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void close() throws Exception |
| 40 | + { |
| 41 | + driver.close(); |
| 42 | + } |
| 43 | + |
| 44 | + public void printGreeting( final String message ) |
| 45 | + { |
| 46 | + try ( Session session = driver.session() ) |
| 47 | + { |
| 48 | + String greeting = session.writeTransaction( new TransactionWork<String>() |
| 49 | + { |
| 50 | + @Override |
| 51 | + public String execute( Transaction tx ) |
| 52 | + { |
| 53 | + Result result = tx.run( "CREATE (a:Greeting) " + |
| 54 | + "SET a.message = $message " + |
| 55 | + "RETURN a.message + ', from node ' + id(a)", |
| 56 | + parameters( "message", message ) ); |
| 57 | + return result.single().get( 0 ).asString(); |
| 58 | + } |
| 59 | + } ); |
| 60 | + System.out.println( greeting ); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public static void main( String... args ) throws Exception |
| 65 | + { |
| 66 | + try ( DriverIntroductionExample greeter = new DriverIntroductionExample( "bolt://localhost:7687", "neo4j", "password" ) ) |
| 67 | + { |
| 68 | + greeter.printGreeting( "hello, world" ); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | +// end::driver-introduction-example[] |
| 73 | + |
| 74 | +// tag::hello-world-output[] |
| 75 | +// hello, world, from node 1234 |
| 76 | +// end::hello-world-output[] |
0 commit comments