呼、博客要落灰了
记录一下Java连接neo4j的问题。
首先是neo4j的下载和配置,基本参考https://blog.csdn.net/appleyk/article/details/79091898
1、下载jdk
2、下载neo4j的community版本或者desktop版本 下载地址:https://neo4j.com/download-center/
3、配置环境变量 NEO4J_HOME=解压地址、path=%NEO4J_HOME%\bin;
4、验证是否安装成功:在cmd中运行neo4j.bat console
5、注册neo4j服务:neo4j install-service
6、注册后就可以使用neo4j start 来启动服务。
然后是在eclipse中新建maven项目
1、利用maven导入neo4j和java连接的包neo4j-java-driver
4.0.0
com.recom
recommender
0.0.1-SNAPSHOT
org.neo4j.driver
neo4j-java-driver
1.7.2
2、编写类测试连接
public classApp {public static voidmain(String[] args) {
Driver driver=GraphDatabase.driver("bolt://localhost:7687",AuthTokens.basic("neo4j", "neo4j"));try(Session session=driver.session()){try(Transaction transaction=session.beginTransaction()){
transaction.run("create(n:A1{NAME:{NAME},TITLE:{TITLE}})", Values.parameters("NAME","james","TITLE","King"));
transaction.success();
}try(Transaction tx=session.beginTransaction()) {
StatementResult result=tx.run("match(a:A1) WHERE a.NAME = {NAME} RETURN a.NAME AS NAME,a.TITLE AS TITLE", Values.parameters("NAME","james"));while(result.hasNext()) {
Record record=result.next();
System.out.println(String.format("%s %s",record.get("TITLE").asString(),record.get("NAME").asString()));
}
}
}
driver.close();
}
}
在测试时发现报以下错误 “The client is unauthorized due to authentication failure.”
解决方法:修改neo4j.conf配置文件,取消验证机制,修改如下: