Ready to get started?

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

 Download Now

Learn more:

Microsoft OneNote Icon OneNote JDBC Driver

Rapidly create and deploy powerful Java applications that integrate with Microsoft OneNote data including Notebooks, Notes, Searches, Tags, and more!

Create a Data Access Object for OneNote Data using JDBI



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

Create a DAO for the OneNote Notebooks 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 MyNotebooksDAO { //insert new data into OneNote @SqlUpdate("INSERT INTO Notebooks (Id, notebook_displayName) values (:id, :notebook_displayName)") void insert(@Bind("id") String id, @Bind("notebook_displayName") String notebook_displayName); //request specific data from OneNote (String type is used for simplicity) @SqlQuery("SELECT notebook_displayName FROM Notebooks WHERE Id = :id") String findnotebook_displayNameById(@Bind("id") String id); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to OneNote

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

OneNote uses the OAuth authentication standard. To authenticate using OAuth, you will need to create an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties. See the Help documentation for more information.

Built-in Connection String Designer

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

java -jar cdata.jdbc.onenote.jar

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

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

jdbc:onenote:OAuthClientId=MyApplicationId; OAuthClientSecret=MySecretKey; CallbackURL=http://localhost:33333;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:onenote:OAuthClientId=MyApplicationId; OAuthClientSecret=MySecretKey; CallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH"); MyNotebooksDAO dao = dbi.open(MyNotebooksDAO.class); //do stuff with the DAO dao.close();

Read OneNote Data

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

//disply the result of our 'find' method String notebook_displayName = dao.findnotebook_displayNameById("Jq74mCczmFXk1tC10GB"); System.out.println(notebook_displayName);

Write OneNote Data

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

//add a new entry to the Notebooks entity dao.insert(newId, newnotebook_displayName);

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