// 导入必要的 Neo4j 包
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Session;
import org.neo4j.driver.Result;
import org.neo4j.driver.Record;
public class Neo4jExample {
// 连接到 Neo4j 数据库并执行一个简单的查询
public static void main(String[] args) {
// 创建驱动程序实例,用于连接到 Neo4j 数据库
Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "password"));
// 打开一个新的会话
try (Session session = driver.session()) {
// 定义要执行的 Cypher 查询
String query = "CREATE (n:Person {name: 'Alice'}) RETURN n";
// 执行查询并获取结果
Result result = session.run(query);
// 遍历结果并打印
while (result.hasNext()) {
Record record = result.next();
System.out.println(record.get("n").get("name").asString());
}
}
// 关闭驱动程序
driver.close();
}
}
GraphDatabase.driver 方法创建了一个驱动程序实例,用于连接到 Neo4j 数据库。这里指定了数据库的 URL 和认证信息(用户名和密码)。driver.session() 打开了一个新的会话,用于执行 Cypher 查询。Person 节点,并返回该节点。session.run(query) 执行查询,并获取结果。如果你需要更多详细的解释或有其他问题,请告诉我!
下一篇:java反射获取class对象
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站