![]() |
VOOZH | about |
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 explains how to build a basic Data Access Object (DAO) and the accompanying code to read Confluence data.
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();
}
Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Confluence.
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.
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.):
To connect to a Server instance, provide the following:
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.
👁 Using the built-in connection string designer to generate a JDBC URL (Salesforce is shown.)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();
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.
Download a free trial of the Confluence Driver to get started:
Download NowLearn more:
👁 Confluence IconRapidly create and deploy powerful Java applications that integrate with Confluence.