Create a Data Access Object for Neo4J Data using JDBI



A brief overview of creating a SQL Object API for Neo4J data in JDBI.

JDBI is a SQL convenience library for Java that exposes two different style APIs, a fluent style and a SQL object style. The CData JDBC Driver for Neo4J integrates connectivity to live Neo4J data in Java applications. By pairing these technologies, you gain simple, programmatic access to Neo4J data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read Neo4J data.

Create a DAO for the Neo4J ProductCategory Entity

The interface below declares the desired behavior for the SQL object to create a single method for each SQL statement to be implemented.

public interface MyProductCategoryDAO { //request specific data from Neo4J (String type is used for simplicity) @SqlQuery("SELECT CategoryName FROM ProductCategory WHERE CategoryOwner = :categoryOwner") String findCategoryNameByCategoryOwner(@Bind("categoryOwner") String categoryOwner); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Neo4J

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Neo4J.

To connect to Neo4j, set the following connection properties:

  • Server: The server hosting the Neo4j instance.
  • Port: The port on which the Neo4j service is running. The provider connects to port 7474 by default.
  • User: The username of the user using the Neo4j instance.
  • Password: The password of the user using the Neo4j instance.

Built-in Connection String Designer

For assistance in constructing the JDBC URL, use the connection string designer built into the Neo4J JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.neo4j.jar

Fill in the connection properties and copy the connection string to the clipboard.

A connection string for Neo4J will typically look like the following:

jdbc:neo4j:Server=localhost;Port=7474;User=my_user;Password=my_password;

Use the configured JDBC URL to obtain an instance of the DAO interface. The particular method shown below will open a handle bound to the instance, so the instance needs to be closed explicitly to release the handle and the bound JDBC connection.

DBI dbi = new DBI("jdbc:neo4j:Server=localhost;Port=7474;User=my_user;Password=my_password;"); MyProductCategoryDAO dao = dbi.open(MyProductCategoryDAO.class); //do stuff with the DAO dao.close();

Read Neo4J Data

With the connection open to Neo4J, simply call the previously defined method to retrieve data from the ProductCategory entity in Neo4J.

//disply the result of our 'find' method String categoryName = dao.findCategoryNameByCategoryOwner("CData Software"); System.out.println(categoryName);

Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Neo4J by integrating with the CData JDBC Driver for Neo4J. Download a free trial and work with live Neo4J data in custom Java applications today.

Ready to get started?

Download a free trial of the Neo4J Driver to get started:

 Download Now

Learn more:

Neo4J Icon Neo4J JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Neo4J.