For example, the following code is stored in a file
<project_dir>/src/main/java/com/mycompany/app/App1.java
:
//The following code sample:
//1) Connects to Oracle Database using Oracle JDBC Driver
//2) Creates a property graph
//3) Adds a vertex to the graph
//4) Runs PGQL queries on the graph and prints the output
//5) Deletes the property graph
package com.mycompany.app;
import java.sql.*;
import oracle.pg.rdbms.*;
import oracle.pg.rdbms.pgql.jdbc.*;
import oracle.pgx.api.*;
public class App1 {
public static void main(String[] args) throws Exception {
String dbConnectString = args[0];
String username = args[1];
String password = args[2];
// 2-tier example: run PGQL against database
DriverManager.registerDriver(new PgqlJdbcRdbmsDriver());
String jdbcUrl = "jdbc:oracle:pgql:@" + dbConnectString;
System.out.println("connecting to " + jdbcUrl);
try (Connection conn = DriverManager.getConnection(jdbcUrl, username, password)) {
conn.setAutoCommit(false);
conn.prepareStatement("CREATE PROPERTY GRAPH test").execute();
conn.commit();
conn.prepareStatement("INSERT INTO test VERTEX v LABELS (Foo) PROPERTIES (v.name = 'Baz')").execute();
conn.commit();
PreparedStatement stmt = conn.prepareStatement("SELECT v.name FROM MATCH (v) ON test");
stmt.execute();
ResultSet rs = stmt.getResultSet();
while (rs.next()) {
System.out.println("name = " + rs.getString(1));
}
conn.prepareStatement("DROP PROPERTY GRAPH test").execute();
}
System.exit(0);
}
}
You can then compile and run the preceding code by navigating
to your project directory and running the following
command:
mvn compile exec:java -Dexec.mainClass="com.mycompany.app.App1"-Dexec.arguments='<db-connect-string>,<username>,<password>'
On successful processing, the code may produce an output
similar to the following. Note, your output may be different depending on
your
<db-connect-string>.
[INFO] --- exec-maven-plugin:3.1.0:java (default-cli) @ my-app ---
connecting to jdbc:oracle:pgql:@myhost:1521/oradb
name = Baz