Ready to get started?

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

Download Now

Create a Data Access Object for Confluence Data using JDBI

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

Create a DAO for the Confluence Pages 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 MyPagesDAO { //request specific data from Confluence (String type is used for simplicity) @SqlQuery("SELECT Name FROM Pages WHERE Id = :id") String findNameById(@Bind("id") String id); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Confluence

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

Obtaining an API Token

An API token is necessary for account authentication. To generate one, login to your Atlassian account and navigate to API tokens > Create API token. The generated token will be displayed.

Connect Using a Confluence Cloud Account

To connect to a Cloud account, provide the following (Note: Password has been deprecated for connecting to a Cloud Account and is now used only to connect to a Server Instance.):

  • User: The user which will be used to authenticate with the Confluence server.
  • APIToken: The API Token associated with the currently authenticated user.
  • Url: The URL associated with your JIRA endpoint. For example, https://yoursitename.atlassian.net.

Connect Using a Confluence Server Instance

To connect to a Server instance, provide the following:

  • User: The user which will be used to authenticate with the Confluence instance.
  • Password: The password which will be used to authenticate with the Confluence server.
  • Url: The URL associated with your JIRA endpoint. For example, https://yoursitename.atlassian.net.

Built-in Connection String Designer

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

java -jar cdata.jdbc.confluence.jar

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

A connection string for Confluence will typically look like the following:

jdbc:confluence:User=admin;APIToken=myApiToken;Url=https://yoursitename.atlassian.net;Timezone=America/New_York;

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:confluence:User=admin;APIToken=myApiToken;Url=https://yoursitename.atlassian.net;Timezone=America/New_York;"); MyPagesDAO dao = dbi.open(MyPagesDAO.class); //do stuff with the DAO dao.close();

Read Confluence Data

With the connection open to Confluence, simply call the previously defined method to retrieve data from the Pages entity in Confluence.

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

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