jdk 1.8, pom.xml中主要引用是:
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-jdbc-driver</artifactId>
<version>3.4.0</version>
</dependency>
单元测试类是:
public class ContactsTest {
@Test
public void testBolt() throws Exception {
String boltUri = "bolt://localhost:7687";
String user = "neo4j";
String password = "xxxx";
Driver driver = GraphDatabase.driver(boltUri, AuthTokens.basic(user, password));;
// Connect 10.1.1.29
// Querying
try (Session session = driver.session()) {
String stmt = session.readTransaction(new TransactionWork<String>() {
@Override
public String execute(Transaction tx) {
StatementResult result = tx.run("MATCH (n:contact) RETURN n.USER_NAME limit 10");
List<Record> list=result.list();
System.out.println("结果:"+list.size());
return list.get(0).toString();
}
});
System.out.println("结果:"+stmt);
}
}
@Test
public void testJdbc() throws Exception {
Class.forName("org.neo4j.jdbc.Driver").newInstance();
// Connect 10.1.1.29
Neo4jConnection con = (Neo4jConnection) DriverManager.getConnection("jdbc:neo4j:http://localhost:7474", "neo4j", "xxxxx");
// Querying
try (Statement stmt = con.createStatement()) {
ResultSet rs = stmt.executeQuery("MATCH (n:contact) RETURN n.USER_NAME limit 10");
while (rs.next()) {
System.out.println(rs.getString("n.USER_NAME"));
}
}
con.close();
}
}