Ready to get started?

Learn more about the CData JDBC Driver for Dynamics 365 or download a free trial:

Download Now

Create a Data Access Object for Dynamics 365 Sales Data using JDBI

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

Create a DAO for the Dynamics 365 Sales 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 Dynamics 365 Sales @SqlUpdate("INSERT INTO Accounts (Name, Name) values (:name, :name)") void insert(@Bind("name") String name, @Bind("name") String name); //request specific data from Dynamics 365 Sales (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 Dynamics 365 Sales

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Dynamics 365 Sales.

Dynamics 365 uses the OAuth authentication standard. See the Getting Started section in the help documentation for a guide to completing the process.

Built-in Connection String Designer

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

java -jar cdata.jdbc.d365sales.jar

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

A connection string for Dynamics 365 Sales will typically look like the following:

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

Read Dynamics 365 Sales Data

With the connection open to Dynamics 365 Sales, simply call the previously defined method to retrieve data from the Accounts entity in Dynamics 365 Sales.

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

Write Dynamics 365 Sales Data

It is also simple to write data to Dynamics 365 Sales, 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 Dynamics 365 Sales by integrating with the CData JDBC Driver for Dynamics 365 Sales. Download a free trial and work with live Dynamics 365 Sales data in custom Java applications today.