在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:agensgraph-jdbc开源软件地址:https://gitee.com/mirrors/agensgraph-jdbc开源软件介绍:AgensGraph JDBC DriverIntroductionAgensGraph JDBC Driver is based on PostgreSQL JDBC Driver and offers additional features to handle graph data. Thus, when users develop Java applications, there is little difference between using the API of AgensGraph JDBC Driver and PostgreSQL JDBC Driver. The only difference is that AgensGraph uses Cypher query language instead of SQL and utilizes graph data as data types such as Usage of Java DriverThis section shows how to use AgensGraph JDBC Driver through examples. Get the DriverYou can download the precompiled driver(jar) from bitnine.net/downloads or use maven as follows. <dependency> <groupId>net.bitnine</groupId> <artifactId>agensgraph-jdbc</artifactId> <version>1.4.2</version></dependency> You may search the latest version on The Central Repository with GroupId and ArtifactId ConnectionIt requires two strings to connect AgensGraph using the driver like other JDBC drivers do. These are the name of the driver class and a connection string.
The following is an example to connect AgensGraph. 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); ... }} Retrieving DataThe following is an example of retrieving graph data using ...import net.bitnine.agensgraph.graph.Vertex;...public class AgensGraphTest { public static void main(String[] args) { ... 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.getString("name")); System.out.println(friend.getInt("age")); } }} Creating DataThe following example shows how to create a vertex with ...import net.bitnine.agensgraph.util.Jsonb;import net.bitnine.agensgraph.util.JsonbUtil;import java.sql.PreparedStatement;...public class AgensGraphTest { public static void main(String[] args) { ... PreparedStatement pstmt = conn.prepareStatement("CREATE (:person ?)"); Jsonb j = JsonbUtil.createObjectBuilder() .add("name", "John") .add("from", "USA") .add("age", 17) .build(); pstmt.setObject(1, j); pstmt.execute(); }} The following is the actual query to be executed.
Using Named Parameter...import net.bitnine.agensgraph.jdbc.AgConnection;import net.bitnine.agensgraph.jdbc.AgPreparedStatement;...public class AgensGraphTest { public static void main(String[] args) { ... aconn = conn.unwrap(AgConnection.class); AgPreparedStatement apstmt = aconn.prepareNamedParameterStatement("CREATE ($data)"); Jsonb j = JsonbUtil.createObjectBuilder() .add("id", 7) .add("enabled", true) .add("day", JsonbUtil.createArray("Sat", "Sun")) .build(); apstmt.setObject("data", j); apstmt.execute(); }} Graph Object ClassesThis section is a brief explanation of Java class to support graph data.
|
请发表评论