Create a Data Access Object for Dynamics 365 Business Central Data using JDBI



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

Create a DAO for the Dynamics 365 Business Central 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 Business Central @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 Business Central (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 Business Central

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

To authenticate to Dynamics 365 Business Central, you must provide the User and AccessKey properties.

To obtain the User and AccessKey values, navigate to the Users page in Dynamics 365 Business Central and then click on Edit. The User Name and Web Service Access Key values are what you will enter as the User and AccessKey connection string properties. Note that the User Name is not your email address. It is a shortened user name.

To connect to data, specify OrganizationUrl. If you have multiple companies in your organization, you must also specify the Company to indicate which company you would like to connect to. Company does not need to be specified if you have only one company.

Built-in Connection String Designer

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

java -jar cdata.jdbc.d365businesscentral.jar

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

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

jdbc:d365businesscentral:OrganizationUrl=https://myaccount.financials.dynamics.com/;

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

Read Dynamics 365 Business Central Data

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

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

Write Dynamics 365 Business Central Data

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

Ready to get started?

Download a free trial of the Dynamics 365 Business Central Driver to get started:

 Download Now

Learn more:

Dynamics 365 Business Central (NAV) Icon Dynamics 365 Business Central JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Dynamics 365 Business Central account data including Items, Sales Orders, Purchase Orders, and more!