Ready to get started?

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

Download Now

Connect to SAP Netweaver Gateway Data in JRuby

Create a simple JRuby app with access to live SAP Netweaver Gateway data.

JRuby is a high-performance, stable, fully threaded Java implementation of the Ruby programming language. The CData JDBC Driver for SAP Netweaver Gateway makes it easy to integrate connectivity to live SAP Netweaver Gateway data in JRuby. This article shows how to create a simple JRuby app that connects to SAP Netweaver Gateway data, executes a query, and displays the results.

Configure a JDBC Connection to SAP Netweaver Gateway Data

Before creating the app, note the installation location for the JAR file for the JDBC Driver (typically C:\Program Files\CData\CData JDBC Driver for SAP Netweaver Gateway\lib).

JRuby natively supports JDBC, so you can easily connect to SAP Netweaver Gateway and execute SQL queries. Initialize the JDBC connection with the getConnection function of the java.sql.DriverManager class.

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.

Below is a typical JDBC connection string for SAP Netweaver Gateway:

jdbc:sapgateway:User=user;Password=password;URL=https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/;InitiateOAuth=GETANDREFRESH

Create a JRuby App with Connectivity to SAP Netweaver Gateway Data

Create a new Ruby file (for example: SAPGatewaySelect.rb) and open it in a text editor. Copy the following code into your file:

require 'java' require 'rubygems' require 'C:/Program Files/CData/CData JDBC Driver for SAP Netweaver Gateway 2018/lib/cdata.jdbc.sapgateway.jar' url = "jdbc:sapgateway:User=user;Password=password;URL=https://sapes5.sapdevcenter.com/sap/opu/odata/IWBEP/GWSAMPLE_BASIC/;InitiateOAuth=GETANDREFRESH" conn = java.sql.DriverManager.getConnection(url) stmt = conn.createStatement rs = stmt.executeQuery("SELECT ProductID, Quantity FROM SalesOrderLineItems") while (rs.next) do puts rs.getString(1) + ' ' + rs.getString(2) end

With the file completed, you are ready to display your SAP Netweaver Gateway data with JRuby. To do so, simply run your file from the command line:

jruby -S SAPGatewaySelect.rb

Writing SQL-92 queries to SAP Netweaver Gateway allows you to quickly and easily incorporate SAP Netweaver Gateway data into your own JRuby applications. Download a free trial today!