Create a Data Access Object for Spotify Data using JDBI

Jerod Johnson
Jerod Johnson
Director, Technology Evangelism
A brief overview of creating a SQL Object API for Spotify 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 Spotify integrates connectivity to live Spotify data in Java applications. By pairing these technologies, you gain simple, programmatic access to Spotify data. This article explains how to build a basic Data Access Object (DAO) and the accompanying code to read Spotify data.

Create a DAO for the Spotify Albums 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 MyAlbumsDAO {
  //request specific data from Spotify (String type is used for simplicity)
  @SqlQuery("SELECT  FROM Albums WHERE Id = :id")
  String findById(@Bind("id") String id);

  /*
   * close with no args is used to close the connection
   */
  void close();
}

Open a Connection to Spotify

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

Using OAuth Authentication

Spotify uses OAuth 2.0 for authentication. You will need to create an application in the Spotify Developer Dashboard to obtain your client credentials.

Setting Up Your Spotify Application

  1. Visit the Spotify Developer Dashboard.
  2. Log in with your Spotify account and click Create app.
  3. Provide an app name, description, and set a Redirect URI (e.g.,
    http://localhost:33333
    for desktop applications).
  4. Copy your Client ID and Client Secret from the app settings.

Connection Properties

After setting the following connection properties, you are ready to connect:

  • AuthScheme: Set this to OAuth.
  • InitiateOAuth: Set this to GETANDREFRESH. You can use InitiateOAuth to manage the process to obtain the OAuthAccessToken.
  • OAuthClientId: Set this to your Spotify application's Client ID.
  • OAuthClientSecret: Set this to your Spotify application's Client Secret.
  • Scope: Set this to the required OAuth scopes (space-separated). The default includes all read scopes needed for the tables in this profile.
  • CallbackURL: Set this to the Redirect URI configured in your Spotify application (e.g., http://localhost:33333).

Example Connection String

Profile=C:\profiles\Spotify.apip;AuthScheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;CallbackURL=http://localhost:33333;

Available OAuth Scopes

  • user-read-private: Read access to user's subscription details and explicit content settings.
  • user-read-email: Read access to user's email address.
  • user-library-read: Read access to a user's saved tracks, albums, episodes, shows, and audiobooks.
  • playlist-read-private: Read access to user's private playlists.
  • playlist-read-collaborative: Read access to collaborative playlists the user follows.
  • user-follow-read: Read access to the list of artists the current user follows.
  • user-read-playback-state: Read access to a user's player state (device, current track, progress).
  • user-read-currently-playing: Read access to a user's currently playing content.
  • user-read-playback-history: Read access to a user's recently played tracks.
  • user-top-read: Read access to a user's top artists and tracks.

Built-in Connection String Designer

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

java -jar cdata.jdbc.api.jar

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

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

jdbc:api:Profile=C:\profiles\Spotify.apip;AuthScheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;CallbackURL=http://localhost:33333;

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:api:Profile=C:\profiles\Spotify.apip;AuthScheme=OAuth;InitiateOAuth=GETANDREFRESH;OAuthClientId=your_client_id;OAuthClientSecret=your_client_secret;CallbackURL=http://localhost:33333;");
MyAlbumsDAO dao = dbi.open(MyAlbumsDAO.class);

//do stuff with the DAO

dao.close();

Read Spotify Data

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

//disply the result of our 'find' method
String  = dao.findById("4aawyAB9vmqN3uQ7FjRGTy");
System.out.println();

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

Ready to get started?

Connect to live data from Spotify with the API Driver

Connect to Spotify