java neo4j_java连接neo4j

该博客介绍了如何使用Java连接到Neo4j数据库。通过加载Neo4j JDBC驱动,建立连接,执行查询并获取结果集。代码示例中展示了如何获取节点数据,并将其解析为JSON对象进行操作。
摘要由CSDN通过智能技术生成

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.SQLException;

import java.sql.Statement;

import com.google.gson.JsonObject;

import com.google.gson.JsonParser;

import org.neo4j.jdbc.Driver;

//Class.forName("org.neo4j.jdbc.Driver");

//Connect

public class DBUtil {

Connection con;

Statement stmt;

ResultSet rs;

public ResultSet rs(String sqlstring) throws SQLException {

try {

Class.forName("org.neo4j.jdbc.Driver").newInstance();

this.con = DriverManager.getConnection("jdbc:neo4j:http://localhost:7474/","用户名","密码");

this.stmt = con.createStatement();

this.rs = stmt.executeQuery(sqlstring);

} catch (InstantiationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IllegalAccessException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (ClassNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return rs;

}

public void close () {

if (this.rs != null) {

try {

this.rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (this.stmt != null) {

try {

this.stmt.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (this.con != null) {

try {

this.con.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

public static void main(String[] args) throws SQLException {

// TODO Auto-generated method stub

DBUtil db=new DBUtil();

ResultSet rs=db.rs("match (n) return n limit 10");

while(rs.next())

{

String jsonstring=rs.getString("n");

System.out.println(jsonstring);

JsonObject jsonObject = (JsonObject) new JsonParser().parse(jsonstring);

System.out.println(jsonObject.get("ff"));

}

db.close();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值