Ready to get started?

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

 Download Now

Learn more:

GitHub Icon GitHub JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with GitHub.

Create a Data Access Object for GitHub Data using JDBI



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

Create a DAO for the GitHub Users 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 MyUsersDAO { //insert new data into GitHub @SqlUpdate("INSERT INTO Users (UserLogin, Email) values (:userLogin, :email)") void insert(@Bind("userLogin") String userLogin, @Bind("email") String email); //request specific data from GitHub (String type is used for simplicity) @SqlQuery("SELECT Email FROM Users WHERE UserLogin = :userLogin") String findEmailByUserLogin(@Bind("userLogin") String userLogin); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to GitHub

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

GitHub uses the OAuth 2 authentication standard. To authenticate using OAuth, you will need to create an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties. See the Getting Started chapter of the CData help documentation for an authentication guide.

Built-in Connection String Designer

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

java -jar cdata.jdbc.github.jar

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

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

jdbc:github:OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:portNumber;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:github:OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:portNumber;InitiateOAuth=GETANDREFRESH"); MyUsersDAO dao = dbi.open(MyUsersDAO.class); //do stuff with the DAO dao.close();

Read GitHub Data

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

//disply the result of our 'find' method String email = dao.findEmailByUserLogin("mojombo"); System.out.println(email);

Write GitHub Data

It is also simple to write data to GitHub, using the previously defined method.

//add a new entry to the Users entity dao.insert(newUserLogin, newEmail);

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