Ready to get started?

Download a free trial of the ServiceNow Driver to get started:

 Download Now

Learn more:

ServiceNow Icon ServiceNow JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with ServiceNow data includingSchedules, Timelines, Questions, Syslogs, and more!

Create a Data Access Object for ServiceNow Data using JDBI



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

Create a DAO for the ServiceNow incident 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 MyincidentDAO { //request specific data from ServiceNow (String type is used for simplicity) @SqlQuery("SELECT priority FROM incident WHERE category = :category") String findpriorityBycategory(@Bind("category") String category); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to ServiceNow

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

ServiceNow uses the OAuth 2.0 authentication standard. To authenticate using OAuth, you will need to register an OAuth app with ServiceNow to obtain the OAuthClientId and OAuthClientSecret connection properties. In addition to the OAuth values, you will need to specify the Instance, Username, and Password connection properties.

See the "Getting Started" chapter in the help documentation for a guide on connecting to ServiceNow.

Built-in Connection String Designer

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

java -jar cdata.jdbc.servicenow.jar

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

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

jdbc:servicenow:OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;Username=MyUsername;Password=MyPassword;Instance=MyInstance;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:servicenow:OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;Username=MyUsername;Password=MyPassword;Instance=MyInstance;InitiateOAuth=GETANDREFRESH"); MyincidentDAO dao = dbi.open(MyincidentDAO.class); //do stuff with the DAO dao.close();

Read ServiceNow Data

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

//disply the result of our 'find' method String priority = dao.findpriorityBycategory("request"); System.out.println(priority);

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