Create a Data Access Object for Microsoft Dataverse Data using JDBI



A brief overview of creating a SQL Object API for Microsoft Dataverse 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 Microsoft Dataverse integrates connectivity to live Microsoft Dataverse data in Java applications. By pairing these technologies, you gain simple, programmatic access to Microsoft Dataverse data. This article walks through building a basic Data Access Object (DAO) and the accompanying code to read and write Microsoft Dataverse data.

About Microsoft Dataverse Data Integration

CData provides the easiest way to access and integrate live data from Microsoft Dataverse (formerly the Common Data Service). Customers use CData connectivity to:

  • Access both Dataverse Entities and Dataverse system tables to work with exactly the data they need.
  • Authenticate securely with Microsoft Dataverse in a variety of ways, including Azure Active Directory, Azure Managed Service Identity credentials, and Azure Service Principal using either a client secret or a certificate.
  • Use SQL stored procedures to manage Microsoft Dataverse entities - listing, creating, and removing associations between entities.

CData customers use our Dataverse connectivity solutions for a variety of reasons, whether they're looking to replicate their data into a data warehouse (alongside other data sources)or analyze live Dataverse data from their preferred data tools inside the Microsoft ecosystem (Power BI, Excel, etc.) or with external tools (Tableau, Looker, etc.).


Getting Started


Create a DAO for the Microsoft Dataverse Accounts 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 MyAccountsDAO { //insert new data into Microsoft Dataverse @SqlUpdate("INSERT INTO Accounts (Name, Name) values (:name, :name)") void insert(@Bind("name") String name, @Bind("name") String name); //request specific data from Microsoft Dataverse (String type is used for simplicity) @SqlQuery("SELECT Name FROM Accounts WHERE Name = :name") String findNameByName(@Bind("name") String name); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Microsoft Dataverse

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

You can connect without setting any connection properties for your user credentials. Below are the minimum connection properties required to connect.

  • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to avoid repeating the OAuth exchange and manually setting the OAuthAccessToken.
  • OrganizationUrl: Set this to the organization URL you are connecting to, such as https://myorganization.crm.dynamics.com.
  • Tenant (optional): Set this if you wish to authenticate to a different tenant than your default. This is required to work with an organization not on your default Tenant.

When you connect the Common Data Service OAuth endpoint opens in your default browser. Log in and grant permissions. The OAuth process completes automatically.

Built-in Connection String Designer

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

java -jar cdata.jdbc.cds.jar

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

A connection string for Microsoft Dataverse will typically look like the following:

jdbc:cds:OrganizationUrl=https://myaccount.crm.dynamics.com/InitiateOAuth=GETANDREFRESH

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:cds:OrganizationUrl=https://myaccount.crm.dynamics.com/InitiateOAuth=GETANDREFRESH"); MyAccountsDAO dao = dbi.open(MyAccountsDAO.class); //do stuff with the DAO dao.close();

Read Microsoft Dataverse Data

With the connection open to Microsoft Dataverse, simply call the previously defined method to retrieve data from the Accounts entity in Microsoft Dataverse.

//disply the result of our 'find' method String name = dao.findNameByName("MyAccount"); System.out.println(name);

Write Microsoft Dataverse Data

It is also simple to write data to Microsoft Dataverse, using the previously defined method.

//add a new entry to the Accounts entity dao.insert(newName, newName);

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

Ready to get started?

Download a free trial of the Microsoft Dataverse Driver to get started:

 Download Now

Learn more:

Microsoft Dataverse Icon Microsoft Dataverse JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Microsoft Dataverse.