Ready to get started?

Download a free trial of the Dynamics NAV Driver to get started:

 Download Now

Learn more:

Dynamics NAV Icon Dynamics NAV JDBC Driver

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

Create a Data Access Object for Dynamics NAV Data using JDBI



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

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

Open a Connection to Dynamics NAV

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

Before you can connect, OData Services will need to be enabled on the server. Once OData Services are enabled, you will be able to query any Services that are published on the server.

The User and Password properties, under the Authentication section, must be set to valid Dynamics NAV user credentials. In addition, you will need to specify a URL to a valid Dynamics NAV server organization root and a ServerInstance. If there is not a Service Default Company for the server, you will need to set the Company as well.

Built-in Connection String Designer

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

java -jar cdata.jdbc.dynamicsnav.jar

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

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

jdbc:dynamicsnav:http://myserver:7048;User=myserver\Administrator;Password=admin;ServerInstance=DYNAMICSNAV71;

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:dynamicsnav:http://myserver:7048;User=myserver\Administrator;Password=admin;ServerInstance=DYNAMICSNAV71;"); MyCustomerDAO dao = dbi.open(MyCustomerDAO.class); //do stuff with the DAO dao.close();

Read Dynamics NAV Data

With the connection open to Dynamics NAV, simply call the previously defined method to retrieve data from the Customer entity in Dynamics NAV.

//disply the result of our 'find' method String prices_Including_VAT = dao.findPrices_Including_VATByName("Bob"); System.out.println(prices_Including_VAT);

Write Dynamics NAV Data

It is also simple to write data to Dynamics NAV, using the previously defined method.

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

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