Ready to get started?

Learn more about the CData JDBC Driver for SAP Netweaver Gateway or download a free trial:

Download Now

Create a Data Access Object for SAP Netweaver Gateway Data using JDBI

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

Create a DAO for the SAP Netweaver Gateway SalesOrderLineItems 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 MySalesOrderLineItemsDAO { //insert new data into SAP Netweaver Gateway @SqlUpdate("INSERT INTO SalesOrderLineItems (Quantity, Quantity) values (:quantity, :quantity)") void insert(@Bind("quantity") String quantity, @Bind("quantity") String quantity); //request specific data from SAP Netweaver Gateway (String type is used for simplicity) @SqlQuery("SELECT Quantity FROM SalesOrderLineItems WHERE Quantity = :quantity") String findQuantityByQuantity(@Bind("quantity") String quantity); /* * close with no args is used to close the connection */ void close(); }

Open a Connection to SAP Netweaver Gateway

Collect the necessary connection properties and construct the appropriate JDBC URL for connecting to SAP Netweaver Gateway.

SAP Gateway allows both basic and OAuth 2.0 authentication. You can use basic authentication to connect to your own account, or you can use OAuth to enable other users to retrieve data from your service with their accounts. In addition to authenticating, set the following connection properties to access SAP Gateway tables.

  • Url: Set this to the URL of your environment, or to the full URL of the service. For example, the full URL might appear as: https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/. In this example, the environment URL would just be: https://sapes5.sapdevcenter.com.
  • Namespace: Set the appropriate Service Namespace. In the example above, IWBEP is the namespace. It is optional if the full URL to the service is specified.
  • Service: Set this to the service you want to retrieve data from. In the example above, the service is GWSAMPLE_BASIC. It is not required if the full URL is specified.

Authenticate via Basic Authentication

In basic authentication, you use your login credentials to connect. Set the following properties:

  • User: This is the username you use to log in to SAP Gateway.
  • Password: This is the password you use to log in to SAP Gateway.

Authenticate via OAuth Authentication

You can connect to SAP Gateway using the embedded OAuth connectivity (without setting any additional authentication connection properties). When you connect, the OAuth endpoint opens in your browser. Log in and grant permissions to complete the OAuth process. See the OAuth section in the online Help documentation for more information on other OAuth authentication flows.

Built-in Connection String Designer

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

java -jar cdata.jdbc.sapgateway.jar

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

A connection string for SAP Netweaver Gateway will typically look like the following:

jdbc:sapgateway:User=user;Password=password;URL=https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/;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:sapgateway:User=user;Password=password;URL=https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/;InitiateOAuth=GETANDREFRESH"); MySalesOrderLineItemsDAO dao = dbi.open(MySalesOrderLineItemsDAO.class); //do stuff with the DAO dao.close();

Read SAP Netweaver Gateway Data

With the connection open to SAP Netweaver Gateway, simply call the previously defined method to retrieve data from the SalesOrderLineItems entity in SAP Netweaver Gateway.

//disply the result of our 'find' method String quantity = dao.findQuantityByQuantity("15"); System.out.println(quantity);

Write SAP Netweaver Gateway Data

It is also simple to write data to SAP Netweaver Gateway, using the previously defined method.

//add a new entry to the SalesOrderLineItems entity dao.insert(newQuantity, newQuantity);

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