当前位置 : 主页 > 编程语言 > c++ >

Java 通过 JDBC 访问 AgensGraph 图数据库

来源:互联网 收集:自由互联 发布时间:2021-06-30
gistfile1.txt import java.sql.DriverManager;import java.sql.Connection;public class AgensGraphTest { public static void main(String[] args) { Class.forName("net.bitnine.agensgraph.Driver"); String connectionString = "jdbc:agensgraph://127.0
gistfile1.txt
import java.sql.DriverManager;
import java.sql.Connection;

public class AgensGraphTest {
  public static void main(String[] args) {
    Class.forName("net.bitnine.agensgraph.Driver");
    String connectionString = "jdbc:agensgraph://127.0.0.1:5432/agens";
	String username = "test";
	String password = "test";
    Connection conn = DriverManager.getConnection(connectionString, username, password);
    //创建对象
    PreparedStatement pstmt = con.prepareStatement("CREATE (:Person ?)");
    JsonObject john = new JsonObject();
    john.put("name", "John");
    john.put("from", "USA");
    john.put("age", 17);
    pstmt.setObject(1, john);
    pstmt.execute();
    //查询
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(
        "MATCH (:Person {name: 'John'})-[:knows]-(friend:Person)" +
        "RETURN friend");
    while (rs.next()) {
      Vertex friend = (Vertex)rs.getObject(1);
      System.out.println(friend.getProperty().getString("name"));
      System.out.println(friend.getProperty().getInt("age"));
    }
  }
}
网友评论