Ready to get started?

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

Download Now

Create a Data Access Object for Bing Ads Data using JDBI

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

Create a DAO for the Bing 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 Bing Ads @SqlUpdate("INSERT INTO AdGroups (CampaignId, Name) values (:campaignId, :name)") void insert(@Bind("campaignId") String campaignId, @Bind("name") String name); //request specific data from Bing 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 Bing Ads

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Bing Ads.

The Bing Ads APIs use the OAuth 2 standard. To authenticate, you will need valid Bing Ads OAuth credentials and you will need to obtain a developer token. See the Getting Started section in the Bing 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 Bing Ads JDBC Driver. Either double-click the JAR file or execute the jar file from the command-line.

java -jar cdata.jdbc.bingads.jar

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

A connection string for Bing Ads will typically look like the following:

jdbc:bingads: 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:bingads: 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 Bing Ads Data

With the connection open to Bing Ads, simply call the previously defined method to retrieve data from the AdGroups entity in Bing Ads.

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

Write Bing Ads Data

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