Create a Data Access Object for Microsoft Entra ID Data using JDBI

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
A brief overview of creating a SQL Object API for Microsoft Entra ID 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 Entra ID integrates connectivity to live Microsoft Entra ID data in Java applications. By pairing these technologies, you gain simple, programmatic access to Microsoft Entra ID data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read and write Microsoft Entra ID data.

Create a DAO for the Microsoft Entra ID Domains 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 MyDomainsDAO {
  //insert new data into Microsoft Entra ID
  @SqlUpdate("INSERT INTO Domains (isVerified, availabilityStatus) values (:isVerified, :availabilityStatus)")
  void insert(@Bind("isVerified") String isVerified, @Bind("availabilityStatus") String availabilityStatus);

  //request specific data from Microsoft Entra ID (String type is used for simplicity)
  @SqlQuery("SELECT availabilityStatus FROM Domains WHERE isVerified = :isVerified")
  String findavailabilityStatusByisVerified(@Bind("isVerified") String isVerified);

  /*
   * close with no args is used to close the connection
   */
  void close();
}

Open a Connection to Microsoft Entra ID

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

Microsoft Entra ID uses the OAuth authentication standard. To authenticate using OAuth, create an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties. See the OAuth section in the Help documentation for an authentication guide.

Built-in Connection String Designer

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

java -jar cdata.jdbc.azuread.jar

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

Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)

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

jdbc:azuread:OAuthClientId=MyApplicationId;OAuthClientSecret=MySecretKey;CallbackURL=http://localhost:33333;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:azuread:OAuthClientId=MyApplicationId;OAuthClientSecret=MySecretKey;CallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH;");
MyDomainsDAO dao = dbi.open(MyDomainsDAO.class);

//do stuff with the DAO

dao.close();

Read Microsoft Entra ID Data

With the connection open to Microsoft Entra ID, simply call the previously defined method to retrieve data from the Domains entity in Microsoft Entra ID.

//disply the result of our 'find' method
String availabilityStatus = dao.findavailabilityStatusByisVerified("TRUE");
System.out.println(availabilityStatus);

Write Microsoft Entra ID Data

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

//add a new entry to the Domains entity
dao.insert(newisVerified, newavailabilityStatus);

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

Ready to get started?

Download a free trial of the Azure Active Directory Driver to get started:

 Download Now

Learn more:

Azure Active Directory Icon Azure Active Directory JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Microsoft Entra ID data including Devices, Groups, Organizations, Users, and more!