Create a Data Access Object for Microsoft Ads Data using 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 Ads integrates connectivity to live Microsoft Ads data in Java applications. By pairing these technologies, you gain simple, programmatic access to Microsoft Ads data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read and write Microsoft Ads data.
Create a DAO for the Microsoft Ads AdGroups 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 MyAdGroupsDAO {
//insert new data into Microsoft Ads
@SqlUpdate("INSERT INTO AdGroups (CampaignId, Name) values (:campaignId, :name)")
void insert(@Bind("campaignId") String campaignId, @Bind("name") String name);
//request specific data from Microsoft Ads (String type is used for simplicity)
@SqlQuery("SELECT Name FROM AdGroups WHERE CampaignId = :campaignId")
String findNameByCampaignId(@Bind("campaignId") String campaignId);
/*
* close with no args is used to close the connection
*/
void close();
}
Open a Connection to Microsoft Ads
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Microsoft Ads.
The Microsoft Ads APIs use the OAuth 2 standard. To authenticate, you will need valid Microsoft Ads OAuth credentials and obtain a developer token. See the Getting Started section in the Microsoft Ads data provider 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 Ads JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.
java -jar cdata.jdbc.microsoftads.jar
Fill in the connection properties and copy the connection string to the clipboard.
A connection string for Microsoft Ads will typically look like the following:
jdbc:microsoftads: OAuthClientId=MyOAuthClientId; OAuthClientSecret=MyOAuthClientSecret; CallbackURL=http://localhost:portNumber; AccountId=442311; CustomerId=5521444; DeveloperToken=11112332233;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:microsoftads: OAuthClientId=MyOAuthClientId; OAuthClientSecret=MyOAuthClientSecret; CallbackURL=http://localhost:portNumber; AccountId=442311; CustomerId=5521444; DeveloperToken=11112332233;InitiateOAuth=GETANDREFRESH;");
MyAdGroupsDAO dao = dbi.open(MyAdGroupsDAO.class);
//do stuff with the DAO
dao.close();
Read Microsoft Ads Data
With the connection open to Microsoft Ads, simply call the previously defined method to retrieve data from the AdGroups entity in Microsoft Ads.
//disply the result of our 'find' method
String name = dao.findNameByCampaignId("234505536");
System.out.println(name);
Write Microsoft Ads Data
It is also simple to write data to Microsoft Ads, using the previously defined method.
//add a new entry to the AdGroups entity
dao.insert(newCampaignId, newName);
Since the JDBI library is able to work with JDBC connections, you can easily produce a SQL Object API for Microsoft Ads by integrating with the CData JDBC Driver for Microsoft Ads. Download a free trial and work with live Microsoft Ads data in custom Java applications today.