Ready to get started?

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

Download Now

Create a Data Access Object for Microsoft Exchange Data using JDBI

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

Create a DAO for the Microsoft Exchange Contacts 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 MyContactsDAO { //insert new data into Microsoft Exchange @SqlUpdate("INSERT INTO Contacts (BusinnessAddress_City, Size) values (:businnessAddress_City, :size)") void insert(@Bind("businnessAddress_City") String businnessAddress_City, @Bind("size") String size); //request specific data from Microsoft Exchange (String type is used for simplicity) @SqlQuery("SELECT Size FROM Contacts WHERE BusinnessAddress_City = :businnessAddress_City") String findSizeByBusinnessAddress_City(@Bind("businnessAddress_City") String businnessAddress_City); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Microsoft Exchange

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

Specify the User and Password to connect to Exchange. Additionally, specify the address of the Exchange server you are connecting to and the Platform associated with the server.

Built-in Connection String Designer

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

java -jar cdata.jdbc.exchange.jar

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

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

jdbc:exchange:User='myUser@mydomain.onmicrosoft.com';Password='myPassword';Server='https://outlook.office365.com/EWS/Exchange.asmx';Platform='Exchange_Online';

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:exchange:User='myUser@mydomain.onmicrosoft.com';Password='myPassword';Server='https://outlook.office365.com/EWS/Exchange.asmx';Platform='Exchange_Online';"); MyContactsDAO dao = dbi.open(MyContactsDAO.class); //do stuff with the DAO dao.close();

Read Microsoft Exchange Data

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

//disply the result of our 'find' method String size = dao.findSizeByBusinnessAddress_City("Raleigh"); System.out.println(size);

Write Microsoft Exchange Data

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

//add a new entry to the Contacts entity dao.insert(newBusinnessAddress_City, newSize);

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