Create a Data Access Object for Jira Service Management Data using JDBI



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

Create a DAO for the Jira Service Management Requests 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 MyRequestsDAO { //insert new data into Jira Service Management @SqlUpdate("INSERT INTO Requests (CurrentStatus, ReporterName) values (:currentStatus, :reporterName)") void insert(@Bind("currentStatus") String currentStatus, @Bind("reporterName") String reporterName); //request specific data from Jira Service Management (String type is used for simplicity) @SqlQuery("SELECT ReporterName FROM Requests WHERE CurrentStatus = :currentStatus") String findReporterNameByCurrentStatus(@Bind("currentStatus") String currentStatus); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to Jira Service Management

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to Jira Service Management.

You can establish a connection to any Jira Service Desk Cloud account or Server instance.

Connecting with a Cloud Account

To connect to a Cloud account, you'll first need to retrieve an APIToken. To generate one, log in to your Atlassian account and navigate to API tokens > Create API token. The generated token will be displayed.

Supply the following to connect to data:

  • User: Set this to the username of the authenticating user.
  • APIToken: Set this to the API token found previously.

Connecting with a Service Account

To authenticate with a service account, you will need to supply the following connection properties:

  • User: Set this to the username of the authenticating user.
  • Password: Set this to the password of the authenticating user.
  • URL: Set this to the URL associated with your JIRA Service Desk endpoint. For example, https://yoursitename.atlassian.net.

Note: Password has been deprecated for connecting to a Cloud Account and is now used only to connect to a Server Instance.

Accessing Custom Fields

By default, the connector only surfaces system fields. To access the custom fields for Issues, set IncludeCustomFields.

Built-in Connection String Designer

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

java -jar cdata.jdbc.jiraservicedesk.jar

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

A connection string for Jira Service Management will typically look like the following:

jdbc:jiraservicedesk:ApiKey=myApiKey;User=MyUser;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:jiraservicedesk:ApiKey=myApiKey;User=MyUser;InitiateOAuth=GETANDREFRESH"); MyRequestsDAO dao = dbi.open(MyRequestsDAO.class); //do stuff with the DAO dao.close();

Read Jira Service Management Data

With the connection open to Jira Service Management, simply call the previously defined method to retrieve data from the Requests entity in Jira Service Management.

//disply the result of our 'find' method String reporterName = dao.findReporterNameByCurrentStatus("Open"); System.out.println(reporterName);

Write Jira Service Management Data

It is also simple to write data to Jira Service Management, using the previously defined method.

//add a new entry to the Requests entity dao.insert(newCurrentStatus, newReporterName);

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

Ready to get started?

Download a free trial of the Jira Service Management Driver to get started:

 Download Now

Learn more:

Jira Service Management Icon Jira Service Management JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Jira Service Management.